
Delfin Rojas wrote:
I don't understand very well why you have 2 mutexes inside Consumer. Anyway,
"mutex" is for access to the queue data structure. "full_mutex" is for signaling that the consumer is waiting on data. Without "full_mutex" the producer might send a notify that gets missed. Or am I thinking about this incorrectly?
I left only one mutex inside Consumer which I called m_mutex and I renamed the condition from full_condition to m_condition and changed The Consumer::push method to this:
void push(int val) { LOG("push(): about to lock"); lock_type lock(m_mutex); LOG("push(): locked"); queue.push(val); m_condition.notify_all(); };
And all seems to work. I ran it for a little while and producer was generating data (ran it up to 33) and consumer was consuming data (about 17 when producer reached 33).
Ok, I think you're right. This should work. As I noted in another message, mocing the notify to the same scope as the lock protecting the condition was the key.
The boost::thread library includes several examples of this producer/consumer pattern. Look at monitor.cpp and starvephil.cpp in libs/thread/example
Boost.Thread has monitors? Why isn't this documented? -Dave