title: different mutex for push and pop
body: <p>I have a class called 'subscribedQueue'. This class receives its data via its subscribed publishers (plural) calling its push method.</p>
  
  <p>In another thread, this class's pop method is called to receive that data.
  Thus in a sense this class is a kind of buffer between multiple publishers and their subscribers. For the implementation I have based myself on information found about thread-safe queues <a href="http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html" rel="nofollow">here</a>.</p>
  
  <p>Now my question is twofold:</p>
  
  <ul>
  <li><strong>If I would use the same mutex</strong> for pusing and popping values (currently I'm using two differrent mutexes), is it possible for my program to get stuck, waiting on a blocked push?</li>
  <li>If not, how is it possible that both the push and pop methods can get past 'lock(the_same_mutex)'.</li>
  </ul>
  
  <p>My assumption is that, if I would use the same mutex and the program enters the pop method, it will acquire the lock in pop, check if the queue is empty and wait on the condition variable which can never be set in the push method (as the lock is already acquired by pop).</p>
  
  <p>current Code (using two different mutexes):</p>
  
  <pre><code>#include &lt;boost/thread.hpp&gt;
  #include &lt;queue&gt;
  #include "subscriber.h"
  #include "pubdata.h"
  #ifdef DEBUG
  #include &lt;iostream&gt;
  #include &lt;boost/lexical_cast.hpp&gt;
  #endif
  
  namespace PUBLISHSUBSCRIBE
  {
    template&lt;class T&gt;
    class SubscribedQueue: public PUBLISHSUBSCRIBE::Subscriber&lt;T&gt;, private std::queue&lt; PubData&lt;T&gt; &gt;
    {
    public:
      PubData&lt;T&gt;  pop();   //removes the next item from the queue, blocks until the queue is not empty
      void push(const PubData&lt;T&gt; data); //method used by the publisher to push data onto the queue
    private:
      mutable boost::mutex writeMutex_; //only needed for publishing/pushing data
      mutable boost::mutex readMutex_;  //only needed for reading/popping data
      boost::condition_variable notify_;
    };
  
    template&lt;class T&gt;
    PubData&lt;T&gt; SubscribedQueue&lt;T&gt;::pop() { //Blocks until the queue is not empty
      boost::mutex::scoped_lock lock(readMutex_);
      while(std::queue&lt; PubData&lt;T&gt; &gt;::empty())
        notify_.wait(lock); //block until recieving a notification AND the queue is not empty
      PubData&lt;T&gt; head = std::queue&lt; PubData&lt;T&gt; &gt;::front();
      std::queue&lt; PubData&lt;T&gt; &gt;::pop();
  #ifdef DEBUG
      std::string debugOut("pop: " + boost::lexical_cast&lt;std::string&gt;(head) + " - timestamp: " + boost::lexical_cast&lt;std::string&gt;(head.timestamp()) + " - from: " + boost::lexical_cast&lt;std::string&gt;(this) + "\n" );
      std::cout &lt;&lt;debugOut;
  #endif
      lock.unlock();
      return head;
    }
  
    template&lt;class T&gt;
    void SubscribedQueue&lt;T&gt;::push(const PubData&lt;T&gt; data){
      boost::mutex::scoped_lock lock(writeMutex_);
  #ifdef DEBUG
      std::cout &lt;&lt; "published: " &lt;&lt; data &lt;&lt; std::endl;
  #endif
      std::queue&lt; PubData&lt;T&gt; &gt;::push(data);
      lock.unlock();
      notify_.notify_one();
    }
  }
  #endif //SUBSCRIBEDQUEUE_H
  </code></pre>
  
  <p>[edit] what worries me most is this: I have a boost::condition_variable notify_ on which a 'wait until notified' is performed in pop.
  But pop must first lock the mutex, the same mutex that must also be locked in 'push' in order to 'notify' the condition variable.</p>
  
  <p>So won't cause this a deadlock, and why not?</p>