//compile with "g++ -pthread thread.cpp -o main" //note to self: try to use ncurses in the next one #include #include #include #include using namespace std; #ifdef _WIN32 //this part has never been tested and is unlikely to work #include ; void fnsleep(unsigned milliseconds) { Sleep(milliseconds); } #else #include 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; }