fork download
  1. #include <thread> // std::thread, std::this_thread::sleep_for
  2. #include <chrono> // std::chrono::seconds
  3. #include <iostream>
  4.  
  5. void pause_thread(int n)
  6. {
  7. std::this_thread::sleep_for (std::chrono::seconds(n));
  8. std::cout << "pause of " << n << " seconds ended\n";
  9. }
  10.  
  11. int main()
  12. {
  13. std::cout << "Spawning 3 threads...\n";
  14. std::thread t1 (pause_thread,1);
  15. std::thread t2 (pause_thread,2);
  16. std::thread t3 (pause_thread,3);
  17. std::cout << "Done spawning threads. Now waiting for them to join:\n";
  18. t1.join();
  19. t2.join();
  20. t3.join();
  21. std::cout << "All threads joined!\n";
  22.  
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5440KB
stdin
Standard input is empty
stdout
Spawning 3 threads...
Done spawning threads. Now waiting for them to join:
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
All threads joined!