[Boost-bugs] [Boost C++ Libraries] #1950: pthread/condition_variable::wait() unlocks associated mutex prematurely and may miss a notification

Subject: [Boost-bugs] [Boost C++ Libraries] #1950: pthread/condition_variable::wait() unlocks associated mutex prematurely and may miss a notification
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2008-05-26 18:56:08


#1950: pthread/condition_variable::wait() unlocks associated mutex prematurely and
may miss a notification
----------------------------------------------------------------+-----------
 Reporter: Stas Maximov <smaximov_at_[hidden]> | Owner: anthonyw
     Type: Bugs | Status: new
Milestone: Boost 1.36.0 | Component: thread
  Version: Boost 1.35.0 | Severity: Showstopper
 Keywords: pthread_cond_wait condition variable pthread mutex |
----------------------------------------------------------------+-----------
 pthread implementation of class condition_variable implements wait() is as
 follows:

 template<typename lock_type>
 void wait(lock_type& m)
 {
     int res=0;
     {
         detail::interruption_checker check_for_interruption(&cond);
         {
             boost::pthread::pthread_mutex_scoped_lock
 internal_lock(&internal_mutex);
             m.unlock();
             res=pthread_cond_wait(&cond,&internal_mutex);
         }
         m.lock();
     }
     if(res)
     {
         throw condition_error();
     }
 }

 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.

 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.

 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().

--
Ticket URL: <http://svn.boost.org/trac/boost/ticket/1950>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.


This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:57 UTC