title: Boost Interprocess mutexes and condition variables
body: <p>I'm looking at this Boost example code for two processes sharing a mutex and condition variable between them:</p>
  
  <p><a href="https://www.boost.org/doc/libs/1_57_0/doc/html/interprocess/synchronization_mechanisms.html" rel="nofollow noreferrer">https://www.boost.org/doc/libs/1_57_0/doc/html/interprocess/synchronization_mechanisms.html</a></p>
  
  <p>but I don't understand how the mutex-condition variable design here can work.</p>
  
  <p>The initial process calls:</p>
  
  <pre><code>for(int i = 0; i &lt; NumMsg; ++i){
       scoped_lock&lt;interprocess_mutex&gt; lock(data-&gt;mutex);    // Take mutex
       if(data-&gt;message_in){
          data-&gt;cond_full.wait(lock);                        // Wait
       }
       if(i == (NumMsg-1))
          std::sprintf(data-&gt;items, "%s", "last message");
       else
          std::sprintf(data-&gt;items, "%s_%d", "my_trace", i);
  
       //Notify to the other process that there is a message
       data-&gt;cond_empty.notify_one();                        // Notify
  
       //Mark message buffer as full
       data-&gt;message_in = true;
    }
  </code></pre>
  
  <p>and the second process calls:</p>
  
  <pre><code>  bool end_loop = false;
    do{
       scoped_lock&lt;interprocess_mutex&gt; lock(data-&gt;mutex);     // Take mutex
       if(!data-&gt;message_in){
          data-&gt;cond_empty.wait(lock);                        // Wait
       }
       if(std::strcmp(data-&gt;items, "last message") == 0){
          end_loop = true;
       }
       else{
          //Print the message
          std::cout &lt;&lt; data-&gt;items &lt;&lt; std::endl;
          //Notify the other process that the buffer is empty
          data-&gt;message_in = false;
          data-&gt;cond_full.notify_one();                       // Notify         
       }
    }
    while(!end_loop);
  </code></pre>
  
  <p>To call <code>wait()</code> or <code>notify()</code> either process must hold the shared mutex, so if one process is on <code>wait()</code> the other surely cannot call <code>notify()</code>?</p>