uqb
uqb

Reputation: 310

Is this a bug in the C++ conditional_variable?

The following is the source code in the header file of conditional_variable of C++5.2.1. I wonder whether there is a problem in the first wait, where the while testing is not in a critical section of __lock waked from wait again.

 template<typename _Lock, typename _Predicate>
 void wait(_Lock& __lock, _Predicate __p) {
     while (!__p())
         wait(__lock);
 }

template<typename _Lock>
void wait(_Lock& __lock) {
    shared_ptr<mutex> __mutex = _M_mutex;
    unique_lock<mutex> __my_lock(*__mutex);
    _Unlock<_Lock> __unlock(__lock);
    // *__mutex must be unlocked before re-locking __lock so move
    // ownership of *__mutex lock to an object with shorter lifetime.
    unique_lock<mutex> __my_lock2(std::move(__my_lock));
    _M_cond.wait(__my_lock2);
 }

Upvotes: 1

Views: 77

Answers (1)

cshu
cshu

Reputation: 5944

Not a bug. When !__p() is evaluated the mutex in __lock is supposedly still locked by current thread.

I suppose the code is from libstdc++.

Upvotes: 1

Related Questions