fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <thread>
  4.  
  5. using namespace std;
  6.  
  7. // The function we want to execute on the new thread.
  8. void task1(string msg)
  9. {
  10. cout << "task1 says: " << msg;
  11. }
  12.  
  13. int main()
  14. {
  15. // Constructs the new thread and runs it. Does not block execution.
  16. thread t1(task1, "Hello");
  17.  
  18. // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
  19. t1.join();
  20. }
Success #stdin #stdout 0s 82816KB
stdin
Standard input is empty
stdout
task1 says: Hello