title: Boost condition deadlock using wait() in producer-consumer code
body: <p>I have implemented a basic threaded producer-consumer (thread 1 = producer, thread 2 = consumer) using Boost threads and conditions. I am getting stuck in <strong>wait()</strong> indefinitely quite often. I can't really see what could be wrong here. Below is some pseudo-code:</p>
  
  <pre><code>// main class
  class Main {
  public:
    void AddToQueue(...someData...)
    {
      boost::mutex::scoped_lock lock(m_mutex);
      m_queue.push_back(new QueueItem(...someData...));
      m_cond.notify_one(); 
    }
  
    void RemoveQueuedItem(...someCond...)
    {
      // i'm wondering if this could cause the trouble?
      boost::mutex::scoped_lock lock(m_mutex);
      // erase a item matching condition (some code not shown,
      // but should be fairly self-explanatory -- IsMatch()
      // simply looks at a flag of QueueItem
      m_queue.erase(std::remove_if(m_queue.being(), m_queue.end(),
        boost::bind(&amp;Main::IsMatch, this, _1, someCond), m_queue.end());
    }
  
    friend void WorkerThread(Main* m);
  private:      
    boost::ptr_deque&lt;QueueItem&gt; m_queue;
    boost::mutex m_mutex;
    boost::condition m_cond;
  };
  
  // worker thread
  void WorkerThread(Main* m)
  {
    typedef boost::ptr_deque&lt;QueueItem&gt;::auto_type RelType;
    RelType queueItem;
  
    while(!shutDown) {
      { // begin mutex scope
        boost::mutex::scoped_lock lock(m-&gt;m_mutex);
        while(m-&gt;m_queue.empty()) {
          m-&gt;m_cond.wait(lock); // &lt;- stuck here forever quite often!
        }
        queueItem = m-&gt;m_queue-&gt;pop_front(); // pop &amp; take ptr ownership
      } // end mutex scope
  
      // ... do stuff with queueItem
      // ...
      // ... queueItem is deleted when it leaves scope &amp; we loop around
    }
  }
  </code></pre>
  
  <p>Some additional information:</p>
  
  <ul>
  <li>Using Boost v1.44</li>
  <li>Issue is occurring in Linux and Android; I'm not yet sure if it happens in Windows</li>
  </ul>
  
  <p>Any ideas?</p>
  
  <p><strong>UPDATE</strong>:
  I <em>believe</em> I have isolated the issue. I'll update further once confirmed, which hopefully will be tomorrow.</p>
  
  <p><strong>UPDATE 2</strong>:
  It turns out there is no issue in the code described above. I was reliant on a underlying API for AddToQueue() - when processing data in the worker thread &amp; handing it back to the API, it had a circular bug where it would call AddToQueue() again... which is now fixed ;-)</p>