fork download
  1. #include <iostream>
  2. #include <atomic>
  3. #include <thread>
  4.  
  5. std::atomic<int> last=2;
  6. std::atomic<int> last_Value = 0;
  7. std::atomic<bool> running = true;
  8.  
  9. void function_Thread_1()
  10. {
  11. while(running)
  12. {
  13. if(last == 2)
  14. {
  15. last_Value = last_Value + 1;
  16. std::cout << last_Value << std::endl;
  17. last = 1;
  18. }
  19. }
  20. }
  21.  
  22. void function_Thread_2()
  23. {
  24. while(running)
  25. {
  26. if(last == 1)
  27. {
  28. last_Value = last_Value + 1;
  29. std::cout << last_Value << std::endl;
  30. last = 2;
  31. }
  32. }
  33. }
  34.  
  35. int main()
  36. {
  37. std::thread a(function_Thread_1);
  38. std::thread b(function_Thread_2);
  39.  
  40. while(last_Value != 6){}//we want to print 1 to 6
  41.  
  42. running = false;//inform threads we are about to stop
  43.  
  44. a.join();
  45. b.join();//join
  46. return 0;
  47. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:27: error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
In file included from prog.cpp:2:0:
/usr/include/c++/4.7/atomic:594:7: error: declared here
prog.cpp:6:35: error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
In file included from prog.cpp:2:0:
/usr/include/c++/4.7/atomic:594:7: error: declared here
prog.cpp:7:32: error: use of deleted function ‘std::atomic<bool>::atomic(const std::atomic<bool>&)’
In file included from prog.cpp:2:0:
/usr/include/c++/4.7/atomic:480:7: error: declared here
stdout
Standard output is empty