fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <chrono>
  4.  
  5. void foo()
  6. {
  7. // simulate expensive operation
  8. std::this_thread::sleep_for(std::chrono::seconds(1));
  9. }
  10.  
  11. void bar()
  12. {
  13. // simulate expensive operation
  14. std::this_thread::sleep_for(std::chrono::seconds(1));
  15. }
  16.  
  17. int main()
  18. {
  19. std::cout << "starting first helper...\n";
  20. std::thread helper1(foo);
  21.  
  22. std::cout << "starting second helper...\n";
  23. std::thread helper2(bar);
  24.  
  25. std::cout << "wait main thread a 1 second\n";
  26. std::this_thread::sleep_for(std::chrono::seconds(1));
  27.  
  28. std::cout << "waiting for first to finish...\n";
  29. helper1.join();
  30.  
  31. std::cout << "wait main thread a 1 second\n";
  32. std::this_thread::sleep_for(std::chrono::seconds(1));
  33.  
  34. std::cout << "waiting for second to finish...\n";
  35. helper2.join();
  36.  
  37. std::cout << "done!\n";
  38. }
Success #stdin #stdout 0s 4584KB
stdin
Standard input is empty
stdout
starting first helper...
starting second helper...
wait main thread a 1 second
waiting for first to finish...
wait main thread a 1 second
waiting for second to finish...
done!