fork(1) download
  1. #include <iostream>
  2. #include <thread>
  3. void thread_function_ref(int const & x)
  4. {
  5. std::cout<<"thread_function_ref, x = "<<x<<std::endl;
  6. }
  7.  
  8. void thread_function_value(int x)
  9. {
  10. std::cout<<"thread_function_value, x = "<<x<<std::endl;
  11. }
  12.  
  13. void thread_function_non_const(int& x)
  14. {
  15. x++;
  16. std::cout<<"thread_function_non_const, x = "<<x<<std::endl;
  17. }
  18.  
  19. int main()
  20. {
  21. int x = 9;
  22. std::thread threadObj1(thread_function_value, x);
  23. std::thread threadObj2(thread_function_ref, std::ref(x));
  24. std::thread threadObj3(thread_function_non_const, std::ref(x));
  25. std::cout<<"In Main Thread : Before Thread Start x = "<<x<<std::endl;
  26. threadObj1.join();
  27. threadObj2.join();
  28. threadObj3.join();
  29. std::cout<<"In Main Thread : After Thread Joins x = "<<x<<std::endl;
  30. return 0;
  31. }
Success #stdin #stdout 0s 4352KB
stdin
Standard input is empty
stdout
In Main Thread : Before Thread Start x = 9
thread_function_non_const, x = 10
thread_function_ref, x = 10
thread_function_value, x = 9
In Main Thread : After Thread Joins x = 10