When using condition_variable_any
with a recursive_mutex
, will the recursive_mutex
be generally acquirable from other threads while condition_variable_any::wait
is waiting? I'm interested in both Boost and C++11 implementations.
This is the use case I'm mainly concerned about:
void bar();
boost::recursive_mutex mutex;
boost::condition_variable_any condvar;
void foo()
{
boost::lock_guard<boost::recursive_mutex> lock(mutex);
// Ownership level is now one
bar();
}
void bar()
{
boost::unique_lock<boost::recursive_mutex> lock(mutex);
// Ownership level is now two
condvar.wait(lock);
// Does this fully release the recursive mutex,
// so that other threads may acquire it while we're waiting?
// Will the recursive_mutex ownership level
// be restored to two after waiting?
}
By a strict interpretation of the Boost documentation, I concluded that
condition_variable_any::wait
will not generally result in therecursive_mutex
being acquirable by other threads while waiting for notification.So
condvar.wait(lock)
will calllock.unlock
, which in turn callsmutex.unlock
, which decreases the ownership level by one (and not necessarily down to zero).I've written a test program that confirms my above conclusion (for both Boost and C++11):
I hope this helps anyone else facing the same dilemma when mixing
condition_variable_any
andrecursive_mutex
.