title: Using condition variable in a producer-consumer situation
body: <p>I'm trying to learn about condition variables and how to use it in a producer-consumer situation. I have a queue where one thread pushes numbers into the queue while another thread popping numbers from the queue. I want to use the condition variable to signal the consuming thread when there is some data placed by the producing thread. The problem is there are times (or most times) that it only pushes up to two items into the queue then hangs. I have indicated in the produce() function where it stops when running in debug mode. Can anyone help me point out why this is happening?</p>
  
  <p>I have the following global variables:</p>
  
  <pre><code>
  boost::mutex mutexQ;               // mutex protecting the queue
  boost::mutex mutexCond;            // mutex for the condition variable
  boost::condition_variable condQ;
  </code></pre>
  
  <p>Below is my consumer thread:</p>
  
  <pre><code>
  void consume()
  {
      while( !bStop )   // globally declared, stops when ESC key is pressed
      {
          boost::unique_lock lock( mutexCond );
          while( !bDataReady )
          {
              condQ.wait( lock );
          }
  
          // Process data
          if( !messageQ.empty() )
          {
              boost::mutex::scoped_lock lock( mutexQ );
  
              string s = messageQ.front();   
              messageQ.pop();
          }
      }
  }
  </code></pre>
  
  <p>Below is my producer thread:</p>
  
  <pre><code>
  void produce()
  {
      int i = 0;
  
      while(( !bStop ) && ( i &lt MESSAGE ))    // MESSAGE currently set to 10
      {
          stringstream out;
          out &lt&lt i;
          string s = out.str();
  
          boost::mutex::scoped_lock lock( mutexQ );
          messageQ.push( s );
  
          i++;
          {
              boost::lock_guard lock( mutexCond );  // HANGS here
              bDataReady = true;
          }
          condQ.notify_one();
      }
  }
  </code></pre>