Advertisement
Guest User

Untitled

a guest
Jun 10th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1.     void MainLoop() {
  2.         while(true) {
  3.             Mutex&      mutex = condvar_.mutex();
  4.             CondVar&    cv = condvar_.condvar();
  5.             Message*    msg;
  6.  
  7.             // Process all enqueued messages,
  8.             // waiting for next msg if the queue is empty.
  9.             {
  10.                 // Lock the queue
  11.                 MutexLock lock(mutex);
  12.                 // Get a message if there is one
  13.                 msg = queue_.Pop();
  14.                 // If there was no msg in the queue
  15.                 // wait till we get one.
  16.                 // condvar may get a spurious wake,
  17.                 // thus we should wait again if the queue is still empty.
  18.                 while(!msg) {
  19.                     cv.Wait();
  20.                     msg = queue_.Pop();
  21.                 }
  22.                 // Unlock the queue when MutexLock goes out of scope
  23.             }
  24.  
  25.             // Handle the message (a slow activity)
  26.             ProcessMessage(msg);
  27.         }
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement