Boost C++ Libraries: Ticket #1950: pthread/condition_variable::wait() unlocks associated mutex prematurely and may miss a notification https://svn.boost.org/trac10/ticket/1950 <p> pthread implementation of class condition_variable implements wait() is as follows: </p> <p> template&lt;typename lock_type&gt; void wait(lock_type&amp; m) { </p> <blockquote> <p> int res=0; { </p> <blockquote> <p> detail::interruption_checker check_for_interruption(&amp;cond); { </p> <blockquote> <p> boost::pthread::pthread_mutex_scoped_lock internal_lock(&amp;internal_mutex); m.unlock(); res=pthread_cond_wait(&amp;cond,&amp;internal_mutex); </p> </blockquote> <p> } m.lock(); </p> </blockquote> <p> } if(res) { </p> <blockquote> <p> throw condition_error(); </p> </blockquote> <p> } </p> </blockquote> <p> } </p> <p> Each condition variable must have an associated mutex. pthread_cond_wait() must enter with the mutex locked. Having the mutex locked guarantees that no notifications will be missed by the receiving thread. </p> <p> The above implementation releases mutex before entering pthread_cond_wait(). This creates an opportunity for pre-emption of the thread just after the mutex was unlocked, but before pthread has been entered. If another thread pre-empts at this point and tries to notify this condition variable, this notification will be missed by this thread. </p> <p> Substitution of some random, unrelated mutex into pthread_cond_wait() is not a solution. It is the original mutex that is associated with the condition that must be passed to pthread_cond_wait(). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1950 Trac 1.4.3 Anthony Williams Mon, 26 May 2008 20:38:09 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/1950#comment:1 https://svn.boost.org/trac10/ticket/1950#comment:1 <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">invalid</span> </li> </ul> <p> Note that the same internal mutex is locked in both notify_one and notify_all. It is not possible to call pthread_cond_signal or pthread_cond_broadcast through the boost::condition_variable_any interface whilst a waiting thread is in the gap you highlighted. </p> Ticket