Using condition_variable with unique_lock causing periodic crash (GCC 4.7, OSX)
<p>I have some code which I adapted (barely changing it) from the C++ Concurrency In Action book, so would have expected it to work -- only it doesn't.
What I've tried to do is to implement a thread-safe queue that I can then store background jobs for a thread or threads in.
The queue looks like this:</p>

<h1>queue.h</h1>

<pre><code>#pragma once
#include "imgproc/i_queue.h"
#include &lt;memory&gt;
#include &lt;thread&gt;
#include &lt;queue&gt;
#include &lt;mutex&gt;
#include &lt;condition_variable&gt;

using namespace std;

namespace imgproc {

  /* Blocking, concurrency-safe queue. The method that blocks is pop(),
   * which makes the current thread wait until there is a value to pop
   * from the queue.
   */
  template &lt;typename T&gt;
    struct ConcurrentQueue : public IQueueWriter&lt;T&gt;, public IQueueReader&lt;T&gt;
    {
      ConcurrentQueue() {}
      ConcurrentQueue( const ConcurrentQueue &amp; ) = delete;
      ConcurrentQueue &amp; operator= ( const ConcurrentQueue &amp; ) = delete;

      /* Concurrency-safe push to queue.
       */
      virtual void push( shared_ptr&lt;T&gt; val )
      {
        lock_guard&lt;mutex&gt; lk( _mutex );
        _queue.push( val );
        _cvar.notify_one();
      }

      /* Concurrency-safe check if queue empty.
       */
      virtual const bool empty() const
      {
        lock_guard&lt;mutex&gt; lk( _mutex );
        bool result( _queue.empty() );
        return result;
      }

      /* Waiting, concurrency-safe pop of value. If there are no values in
       * the queue, then this method blocks the current thread until there
       * are.
       */
      virtual shared_ptr&lt;T&gt; pop()
      {
        unique_lock&lt;mutex&gt; lk( _mutex );
        _cvar.wait( lk, [ this ] {return ! _queue.empty(); } );
        auto value( _queue.front() );
        _queue.pop();
        return value;
      }

    private:
      mutable mutex _mutex;
      queue&lt;shared_ptr&lt;T&gt;&gt; _queue;
      condition_variable _cvar;
    };

}
</code></pre>

<p>My understanding is that the one mutex there should protect all attempts to access the queue. However, I have a test that crashes about 1 time in 10:</p>

<h1>test-that-crashes-fragment.cpp</h1>

<pre><code>// Should have threads wait until there is a value to pop
TEST_F( ConcurrentQueueTest,
        ShouldHaveThreadsWaitUntilThereIsAValueToPop ) {
  int val( -1 );
  thread t1( [ this, &amp;val ] {
      for ( uint i( 0 ) ; i &lt; 1000 ; ++i );
      val = *_r_queue-&gt;pop();
    } );
  for ( uint i( 0 ) ; i &lt; 1000 ; ++ i ) {
    for ( uint j( 0 ) ; j &lt; 1000 ; ++ j );
    EXPECT_EQ( -1, val );
  }
  _w_queue-&gt;push( make_shared&lt;int&gt;( 27 ) );
  t1.join();
  EXPECT_EQ( 27, val );
  EXPECT_TRUE( _r_queue-&gt;empty() );
}
</code></pre>

<p>The variables <code>_r_queue</code> and <code>_w_queue</code> are just interfaces on the same <code>ConcurrentQueue</code> instance, here.</p>

<p>From hours spent obsessively cout-ing debug info, it looks like the call to <code>pop()</code> is what causes the crash, always (that I've seen) when the <code>_queue member</code> instance variable is empty.
Could anyone offer me any advice as to what I'm doing wrong, here? I've seen other posts asking for help with similar problems, but they always seem to say that conditional variables are the answer -- and I'm trying that!</p>

<p>Or maybe some advice on how I can debug this better to help me solve it?
FWIW, I tried manually implementing a <code>while</code> with a <code>sleep( 1 )</code> in it, and that still periodically crashed, which rather suggests I've managed to get a race condition despite my best efforts -- only I really can't see it.</p>

<p>Thanks very much for any &amp; all help, and I promise I've tried to figure this out before bothering you all with it.</p>

<p>Cheers,
   Doug.</p>
