title: Multi threaded program hangs on condition wait
body: <p>I have the following piece of code. I am using c++11 threads to write a simple multi threaded producer consumer problem.</p>
  
  <pre><code>class W
  {
  public:
      explicit W();
      void p();
      void c();
  private:
      std::deque&lt;std::uint64_t&gt; q;
      std::shared_ptr&lt;std::mutex&gt; m;
      std::shared_ptr&lt;std::condition_variable&gt; cvQEmpty;
      std::shared_ptr&lt;std::condition_variable&gt; cvQFull;
      const std::size_t queue_size;
  };
  
  W::W()
  : m(std::make_shared&lt;std::mutex&gt;()), 
  cvQEmpty(std::make_shared&lt;std::condition_variable&gt;()),
  cvQFull(std::make_shared&lt;std::condition_variable&gt;()),
  queue_size(3)  
  {
  }
  
  void
  W::p()
  {
      while(1)    
      {
          std::unique_lock&lt;std::mutex&gt; lk(*m.get());
          if (q.size() &gt;= queue_size)
          {
              cvQFull-&gt;wait(lk, [this] { return q.size() &lt; queue_size; });
          }
          q.push_back(q.size());
          std::cout &lt;&lt; "Pushed " &lt;&lt; q[q.size() - 1] &lt;&lt; std::endl;
          lk.unlock();
          cvQEmpty-&gt;notify_one();
      }
  }
  
  void
  W::c()
  {
    while (1)
    {
      std::unique_lock&lt;std::mutex&gt; lk(*m.get());
      if (q.empty())
      {
          cvQEmpty-&gt;wait(lk, [this] { return !q.empty(); });
      }
      while(!q.empty())
      {
        const std::uint64_t val = q[0];
        std::cout &lt;&lt; "Output : " &lt;&lt; val &lt;&lt; std::endl;
        q.pop_back();
      }
      lk.unlock();
      cvQFull-&gt;notify_one();
    }
  }
  
  void 
  foo()
  {
      W w;
      std::thread p(&amp;W::p, w);
      std::thread c(&amp;W::c, w);
      c.join();
      p.join();
  } 
  </code></pre>
  
  <p>Both the threads are deadlocked on condition wait.</p>
  
  <p>Could you please tell me where I am going wrong. The program compiles fine without any warnings.</p>
  
  <p>Compiler Used is : g++-5.8</p>