Guest User

Untitled

a guest
Feb 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <atomic>
  3. #include <thread>
  4.  
  5.  
  6. const int COUNT = 300;
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.  
  13. atomic<bool> e1{false}, e2{false}, e3{false};
  14.  
  15. thread t1([&]() {
  16. for(int i=0; i < COUNT; ++i)
  17. {
  18. bool e = true;
  19. while (!e1.compare_exchange_strong(e, false)) e = true;
  20. cout << "A" << endl;
  21. e2.store(true);
  22. }
  23. });
  24.  
  25. thread t2([&]() {
  26. for(int i=0; i < COUNT; ++i)
  27. {
  28. bool e = true;
  29. while (!e2.compare_exchange_strong(e, false)) e = true;
  30. cout << "B" << endl;
  31. e3.store(true);
  32. }
  33. });
  34.  
  35. thread t3([&]() {
  36. for(int i=0; i < COUNT; ++i)
  37. {
  38. bool e = true;
  39. while (!e3.compare_exchange_strong(e, false)) e = true;
  40. cout << "C" << endl;
  41. e1.store(true);
  42. }
  43. });
  44.  
  45. e1.store(true);
  46.  
  47. t1.join();
  48. t2.join();
  49. t3.join();
  50.  
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment