Advertisement
Guest User

std::condition_variable example

a guest
Apr 2nd, 2012
2,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <thread>
  2. #include <iostream>
  3. #include <mutex>
  4. #include <condition_variable>
  5. #include <string>
  6.  
  7. std::condition_variable g_Bell;
  8. std::condition_variable_any g_Door;
  9.  
  10. class Manager
  11. {
  12. public:
  13.     void ComeToWork()
  14.     {
  15.         std::cout << "Hey security, please open the door!\n";
  16.         g_Bell.notify_one();
  17.         std::mutex mutex;
  18.         mutex.lock();
  19.         g_Door.wait(mutex);
  20.         mutex.unlock();
  21.     }
  22. };
  23.  
  24. class Security
  25. {
  26.     static bool m_SectorClear;
  27.     static std::mutex m_SectorMutex;
  28. public:
  29.     static bool SectorClear()
  30.     {
  31.         std::lock_guard<std::mutex> lock(m_SectorMutex);
  32.         return m_SectorClear;
  33.     }
  34.     void NotifyFellows()
  35.     {
  36.         std::lock_guard<std::mutex> lock(m_SectorMutex);
  37.         m_SectorClear = false;
  38.     }
  39.     void WorkHard()
  40.     {
  41.         m_SectorClear = true;
  42.         std::mutex mutex;
  43.         std::unique_lock<std::mutex> lock(mutex);
  44.         while(true)
  45.         {
  46.             if(g_Bell.wait_for(lock, std::chrono::seconds(5)) == std::cv_status::timeout)
  47.                 std::this_thread::sleep_for(std::chrono::seconds(10));
  48.             else
  49.             {
  50.                 NotifyFellows();
  51.                 g_Door.notify_one();
  52.                 std::cout << "Hello Great Manager, your slaves are ready to serve you!\n" << std::endl;
  53.             }
  54.         }
  55.     }
  56. };
  57.  
  58. bool Security::m_SectorClear;
  59. std::mutex Security::m_SectorMutex;
  60.  
  61. class Programmer
  62. {
  63. public:
  64.     void WorkHard()
  65.     {
  66.         std::cout << "Let's write some govnokod!\n" << std::endl;
  67.         int i = 0;
  68.         while(true)
  69.         {
  70.             i++;
  71.             i--;
  72.         }
  73.     }
  74.     void PlayStarcraft()
  75.     {
  76.         while(Security::SectorClear())
  77.             ;//Играем! :)
  78.         WorkHard();// Работаем :(
  79.     }
  80. };
  81.  
  82.  
  83.  
  84. int main()
  85. {
  86.     Manager manager;
  87.     Programmer programmer;
  88.     Security security;
  89.  
  90.     auto managerCall = [&manager]()
  91.     {
  92.         manager.ComeToWork();
  93.     };
  94.     auto programmerCall = [&programmer]()
  95.     {
  96.         programmer.PlayStarcraft();
  97.     };
  98.     auto securityCall = [&security]()
  99.     {
  100.         security.WorkHard();
  101.     };
  102.     std::thread securityThread(securityCall);
  103.     std::thread programmerThread(programmerCall);
  104.     std::this_thread::sleep_for(std::chrono::minutes(1));
  105.     std::thread managerThread(managerCall);
  106.    
  107.     managerThread.join();
  108.     programmerThread.join();
  109.     securityThread.join();
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement