The implementation of std::conditional_variable_any needs (in gcc and clang) a std::shared_ptr.
Inside the wait
methods, the lifetime of the mutex will be extended to the local scope.
template<typename _Lock>
void
wait(_Lock& __lock)
{
shared_ptr<mutex> __mutex = _M_mutex; // <-- Extend lifetime of 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);
}
I wonder, why we need this here? As long as the conditional_variable_any
object lives, the mutex exist. Wouldn't a std::mutex be enough?