Boost logo

Boost Users :

Subject: Re: [Boost-users] Race in boost::condition_variable::wait
From: Chris Stankevitz (chrisstankevitz_at_[hidden])
Date: 2015-12-23 12:19:14


On Wed, Dec 23, 2015 at 7:44 AM, Éric Malenfant
<eric.malenfant_at_[hidden]> wrote:
> Unless I'm missing something, the guard in condition_variable::wait does not
> release the mutex, it is simply there to ensure that it is reacquired on
> exit. The mutex is released by pthread_cond_wait (atomically with the wait
> on the condition).

Eric,

Thank you for your reply.

I believe you are incorrect that the guard only reacquires (and does
not release) the mutex. The line "guard.activate(m);" indeed releases
the user mutex before pthread_cond_wait (see below).

You are correct that pthread_cond_wait atomically releases the mutex
-- but it is releasing the internal boost mutex, not the "external"
user mutex which has already been released by the guard.

I assume that I am missing something, but I cannot figure out what it is.

Chris

===

        template<typename MutexType>
        struct lock_on_exit
        {
            MutexType* m;

            lock_on_exit():
                m(0)
            {}

            void activate(MutexType& m_)
            {
                m_.unlock();
                m=&m_;
            }
            ~lock_on_exit()
            {
                if(m)
                {
                    m->lock();
                }
           }
        };


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net