I'm having some trouble understanding condition variables and their use with mutexes, I hope the community can help me with. Please note, I come from a win32 background, so I'm used with CRITICAL_SECTION, HANDLE, SetEvent, WaitForMultipleObject, etc.
Here's my first attempt at concurrency using the c++11 standard library, it's a modified version of a program example found here.
#include <condition_variable>
#include <mutex>
#include <algorithm>
#include <thread>
#include <queue>
#include <chrono>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::queue<unsigned int> nNumbers;
std::mutex mtxQueue;
std::condition_variable cvQueue;
bool m_bQueueLocked = false;
std::mutex mtxQuit;
std::condition_variable cvQuit;
bool m_bQuit = false;
std::thread thrQuit(
[&]()
{
using namespace std;
this_thread::sleep_for(chrono::seconds(7));
// set event by setting the bool variable to true
// then notifying via the condition variable
m_bQuit = true;
cvQuit.notify_all();
}
);
std::thread thrProducer(
[&]()
{
using namespace std;
int nNum = 0;
unique_lock<mutex> lock( mtxQuit );
while( ( ! m_bQuit ) &&
( cvQuit.wait_for( lock, chrono::milliseconds(10) ) == cv_status::timeout ) )
{
nNum ++;
unique_lock<mutex> qLock(mtxQueue);
cout << "Produced: " << nNum << "
";
nNumbers.push( nNum );
}
}
);
std::thread thrConsumer(
[&]()
{
using namespace std;
unique_lock<mutex> lock( mtxQuit );
while( ( ! m_bQuit ) &&
( cvQuit.wait_for( lock, chrono::milliseconds(10) ) == cv_status::timeout ) )
{
unique_lock<mutex> qLock(mtxQueue);
if( nNumbers.size() > 0 )
{
cout << "Consumed: " << nNumbers.front() << "
";
nNumbers.pop();
}
}
}
);
thrQuit.join();
thrProducer.join();
thrConsumer.join();
return 0;
}
A Few questions about this.
I've read that "any thread that intends to wait on std::condition_variable must acquire an std::unique_lock first."
So I've got a {quit mutex, condition variable & bool} to indicate when quit has been signalled. The producer and consumer threads must each acquire an std::unique_lock as so:
std::unique_lock<std::mutex> lock(m_mtxQuit);
This is confusing the hell out of me. Won't this lock the quit mutex in the first thread, thereby blocking the second? And if that's true, then how does the first thread release the lock, so that the other thread can begin?
Another question: If I change the wait_for() call to wait for zero seconds, that thread is starved. Can someone explain? I'd expect it not to block before executing the while loop (am I correct to assume that a no_timeout is recv'd instead of a timeout?).
How can I call a wait_for() and specify a zero time, so that the wait_for() call doesn't block, instead it just checks the condition and continues?
I'd also be interested to hear about good references on this subject.
See Question&Answers more detail:os