Paste Search Dynamic
Recent pastes
DisplayEvenNumber
  1. #include<iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <condition_variable>
  5.  
  6. std::mutex m;
  7. std::condition_variable condition;
  8. int number{0};
  9.  
  10. void DisplayEvenNumber()
  11. {a
  12.     std::unique_lock<std::mutex> lock(m);
  13.     while(number < 20)
  14.     {
  15.     condition.wait(lock, [](){return number%2 == 1;});
  16.     if(number < 20)
  17.     {
  18.         std::cout << " " << ++number;
  19.     }
  20.     condition.notify_all();
  21.     }
  22. }
  23.  
  24. void DiplayOddNumber()
  25. {
  26.     std::unique_lock<std::mutex> lock(m);
  27.     while(number < 20)
  28.     {
  29.     condition.wait(lock, [](){return number%2 == 0;});
  30.     if(number < 20)
  31.     {
  32.         std::cout << " " << ++number;
  33.     }
  34.     condition.notify_all();
  35.     }
  36.  
  37. }
  38. int main()
  39. {
  40.     std::thread t1(DisplayEvenNumber), t2(DiplayOddNumber);
  41.     t1.join();
  42.     t2.join();
  43.    
  44.     return 0;
  45. }
Parsed in 0.008 seconds