#include #include #include #include using namespace std; const int COUNT = 20; mutex print_mutex; void print(string text) { for (int i = 0; i < COUNT; i++) { // Acquire the mutex lock to protect the printing. unique_lock lock(print_mutex); // Print a line. for (const char &ch : text) cout << ch; cout << '\n'; // Release the print mutex lock. lock.unlock(); // Yield to other threads. this_thread::yield(); } } int main(void) { thread hello_thread(print, "Hello, world!"); thread go_thread(print, "Go Spartans!"); hello_thread.join(); go_thread.join(); cout << endl << "Program done." << endl; return 0; }