Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void MainLoop() {
- while(true) {
- Mutex& mutex = condvar_.mutex();
- CondVar& cv = condvar_.condvar();
- Message* msg;
- // Process all enqueued messages,
- // waiting for next msg if the queue is empty.
- {
- // Lock the queue
- MutexLock lock(mutex);
- // Get a message if there is one
- msg = queue_.Pop();
- // If there was no msg in the queue
- // wait till we get one.
- // condvar may get a spurious wake,
- // thus we should wait again if the queue is still empty.
- while(!msg) {
- cv.Wait();
- msg = queue_.Pop();
- }
- // Unlock the queue when MutexLock goes out of scope
- }
- // Handle the message (a slow activity)
- ProcessMessage(msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement