fork download
  1. #include <iostream>
  2. #include <thread>
  3.  
  4. void job1()
  5. {
  6. static int data = 42;
  7. std::cout << data << std::endl;
  8. ++data;
  9. }
  10.  
  11. void job2()
  12. {
  13. thread_local static int data = 42;
  14. std::cout << data << std::endl;
  15. ++data;
  16. }
  17.  
  18. int main()
  19. {
  20. std::cout << "Job1" << std::endl;
  21. for (int i=0; i<10; ++i)
  22. {
  23. std::thread t{ job1 };
  24. t.join();
  25. }
  26.  
  27. std::cout << "Job2" << std::endl;
  28. for (int i=0; i<10; ++i)
  29. {
  30. std::thread t{ job2 };
  31. t.join();
  32. }
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 17288KB
stdin
Standard input is empty
stdout
Job1
42
43
44
45
46
47
48
49
50
51
Job2
42
42
42
42
42
42
42
42
42
42