Boost C++ Libraries: Ticket #1951: boost::interprocess::named_condition::do_wait() releases mutex prematurely, may miss notification https://svn.boost.org/trac10/ticket/1951 <p> Implementation of boost::interprocess::named_condition::do_wait() is shown below: </p> <p> template &lt;class Lock&gt; void do_wait(Lock&amp; lock) { </p> <blockquote> <p> lock_inverter&lt;Lock&gt; inverted_lock(lock); <em>unlock internal first to avoid deadlock with near simultaneous waits scoped_lock&lt;lock_inverter&lt;Lock&gt; &gt; external_unlock(inverted_lock); scoped_lock&lt;interprocess_mutex&gt; internal_lock(*this-&gt;mutex()); this-&gt;condition()-&gt;wait(internal_lock); </em></p> </blockquote> <p> } </p> <p> Lock lock is associated with the condition variable and must be released only inside wait(). This implementation releases the lock before it enters the wait(). If lock released before wait() is entered, this function may be pre-empted by a notifier thread, just after the lock has been released, but before wait() has been entered. In such case notification will be missed by the wait(). </p> <p> Having a mutex associated with condition variable is a fundamental requirement. Use of another mutex (internal_lock in this case), not known to the notifier is not a solution. Both the receiver and the notifier, both must use the same CV *and* MX. </p> <p> This is like a root cause of hangs in named_condition_test on Fedora9 running on single-cpu system under vmware-server-1.0.5. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1951 Trac 1.4.3 Stas Maximov <smaximov@…> Tue, 27 May 2008 01:53:56 GMT attachment set https://svn.boost.org/trac10/ticket/1951 https://svn.boost.org/trac10/ticket/1951 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">named_condition.hpp.patch</span> </li> </ul> Ticket Stas Maximov <smaximov@…> Tue, 27 May 2008 01:56:51 GMT <link>https://svn.boost.org/trac10/ticket/1951#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1951#comment:1</guid> <description> <p> Update on the bug report. </p> <p> Hang in regression test named_condition_test has been tracked down to a missed notification due to release of external mutex before internal mutex has been acquired. </p> <p> Attached patch fixes the problem by acquiring internal mutex before releasing the external one. This eliminates potential race with a notifier and thus avoids missed notifications. </p> <p> As before, release of internal mutex is done prior to re-acquisition of external mutex. This is to avoid deadlock with another waiter on the same condition variable. Exception safety has been preserved. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Tue, 27 May 2008 16:39:48 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/1951#comment:2 https://svn.boost.org/trac10/ticket/1951#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Applied patch in revision 45814. Thanks for the report. </p> Ticket