first commit
This commit is contained in:
21
thread test/Makefile
Normal file
21
thread test/Makefile
Normal file
@@ -0,0 +1,21 @@
|
||||
# Compiler and flags
|
||||
CXX = g++
|
||||
CXXFLAGS = -pthread -Wall -Wextra -O2 -std=c++17
|
||||
|
||||
# Linker flags
|
||||
LDFLAGS = -lcurses
|
||||
|
||||
# Default target
|
||||
all: thread threadncurses
|
||||
|
||||
# Build rule
|
||||
thread: thread.cpp
|
||||
$(CXX) $(CXXFLAGS) thread.cpp -o thread $(LDFLAGS)
|
||||
|
||||
threadncurses: threadncurses.cpp
|
||||
$(CXX) $(CXXFLAGS) threadncurses.cpp -o threadncurses $(LDFLAGS)
|
||||
|
||||
# Clean build artifacts
|
||||
clean:
|
||||
rm -f thread threadncurses
|
||||
|
||||
BIN
thread test/thread
Executable file
BIN
thread test/thread
Executable file
Binary file not shown.
49
thread test/thread.cpp
Normal file
49
thread test/thread.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
//compile with "g++ -pthread thread.cpp -o main"
|
||||
//note to self: try to use ncurses in the next one
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
using namespace std;
|
||||
|
||||
#ifdef _WIN32
|
||||
//this part has never been tested and is unlikely to work
|
||||
#include <windows.h>;
|
||||
void fnsleep(unsigned milliseconds)
|
||||
{
|
||||
Sleep(milliseconds);
|
||||
}
|
||||
|
||||
#else
|
||||
#include <unistd.h>
|
||||
void fnsleep(unsigned milliseconds)
|
||||
{
|
||||
usleep(milliseconds * 1000);
|
||||
}
|
||||
#endif
|
||||
|
||||
void helloWorld(int y)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
//a terrible, awful way of getting linux terminal text colors without ncurses.
|
||||
cout << "\033[1;31mbold red text\033[0m\n";
|
||||
cout << "\x1B[32mAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\x1B[0m\n";
|
||||
cout << "Hello world #" << i + 1 << endl;
|
||||
fnsleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
thread thread_object(helloWorld, 1);
|
||||
cout << "This string gets printed after running the hello world function but prints BEFORE helloworld prints. That means this program uses threads" << endl;
|
||||
//use detach to make the thread run asychronoussly. use join to WAIT HERE until its finished. Try it out! (i tested both cases and it works on my machine)
|
||||
//thread_object.detach();
|
||||
thread_object.join();
|
||||
cout << "Please enter your name: " << endl;
|
||||
string name;
|
||||
cin >> name;
|
||||
cout << name << " is a [redacted for academic purposes]" << endl;
|
||||
return 0;
|
||||
}
|
||||
BIN
thread test/threadncurses
Executable file
BIN
thread test/threadncurses
Executable file
Binary file not shown.
90
thread test/threadncurses.cpp
Normal file
90
thread test/threadncurses.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
//compile with "g++ -pthread threadncurses.cpp -o main -lncurses"
|
||||
//the same as thread.cpp but it uses ncurses instead. This proves that ncurses can work even if its in a seperate thread
|
||||
//this is a very functional example of ncurses and asynchronous threading working in the same program
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <ncurses.h>
|
||||
using namespace std;
|
||||
|
||||
#ifdef _WIN32
|
||||
//this part has never been tested and is unlikely to work
|
||||
#include <windows.h>;
|
||||
void fnsleep(unsigned milliseconds)
|
||||
{
|
||||
Sleep(milliseconds);
|
||||
}
|
||||
|
||||
#else
|
||||
#include <unistd.h>
|
||||
void fnsleep(unsigned milliseconds)
|
||||
{
|
||||
usleep(milliseconds * 1000);
|
||||
}
|
||||
#endif
|
||||
|
||||
void helloWorld(int y)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
//store the cursor positions here
|
||||
int y, x;
|
||||
int yy, xx;
|
||||
int xBefore;
|
||||
int yBefore;
|
||||
start_color();
|
||||
init_pair(1, COLOR_RED, COLOR_BLUE);
|
||||
if (i != 0)
|
||||
{
|
||||
//print the hello world lines
|
||||
getyx(stdscr, yBefore, xBefore);
|
||||
move(2, 0);
|
||||
clrtoeol();
|
||||
mvprintw(2, 0, "Hello world #");
|
||||
int icantthinkofanameforthis = i + 1;
|
||||
printw(to_string(icantthinkofanameforthis).c_str());//an annoying but reliable way to get this to work
|
||||
printw("\n");
|
||||
move(yBefore, xBefore); //move the cursor back to where is was before to allow for a fluid typing experience (i.e. its possible to mess this part up if you do it wrong)
|
||||
}
|
||||
else
|
||||
{
|
||||
attron(COLOR_PAIR(1));
|
||||
attron(A_BOLD);
|
||||
getyx(stdscr, yy, xx);
|
||||
printw("bold red text\n");
|
||||
attroff(COLOR_PAIR(1));
|
||||
printw("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
|
||||
attroff(A_BOLD);
|
||||
printw("Hello world #");
|
||||
int icantthinkofanameforthis = i + 1;
|
||||
printw(to_string(icantthinkofanameforthis).c_str());//an annoying but reliable way to get this to work
|
||||
printw("\n");
|
||||
move(3, 25); //the default text-entry position
|
||||
}
|
||||
|
||||
refresh();
|
||||
fnsleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
initscr();
|
||||
use_default_colors();
|
||||
move(3, 0);
|
||||
printw("Please enter your name: \n");
|
||||
move(0, 0);
|
||||
|
||||
thread thread_object(helloWorld, 1);
|
||||
thread_object.detach();
|
||||
|
||||
char *name = new char[99];
|
||||
getstr(name);
|
||||
printw(name);
|
||||
printw(" is a [redacted for academic purposes]\n");
|
||||
printw("Press any key to continue");
|
||||
getch();
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user