#include #include #include #include using namespace std; void sleep_spin(int delay) { for (int i = 0; i < 4; i++) { cout << " sleep_spin(" << delay << ") sleeping..." << endl; sleep(delay); cout << " sleep_spin(" << delay << ") done sleeping." << endl; } } void thread_start(int delay) { cout << "[child thread] I'm alive!" << endl; cout << "[child thread] calling sleep_spin(" << delay << ")" << endl; sleep_spin(delay); cout << "[child thread] done." << endl; } int main(int argc, char **argv) { cout << "[parent thread] about to create thread..." << endl; // Create a child thread, passing it 1 as its argument. thread tr = thread(thread_start, 1); // wait for thread to finish tr.join(); cout << "[parent thread] joined thread" << endl; // do it again, but this time detach from thread thread tr2 = thread(thread_start, 1); tr2.detach(); cout << "[parent thread] done." << endl; return EXIT_SUCCESS; }