c++multithreadingc++11boostrecursive-mutex

Behavior of condition_variable_any when used with a recursive_mutex?


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?
}

Solution

  • By a strict interpretation of the Boost documentation, I concluded that condition_variable_any::wait will not generally result in the recursive_mutex being acquirable by other threads while waiting for notification.

    Class condition_variable_any

    template<typename lock_type> void wait(lock_type& lock)

    Effects:

    Atomically call lock.unlock() and blocks the current thread. The thread will unblock when notified by a call to this->notify_one() or this->notify_all(), or spuriously. When the thread is unblocked (for whatever reason), the lock is reacquired by invoking lock.lock() before the call to wait returns. The lock is also reacquired by invoking lock.lock() if the function exits with an exception.

    So condvar.wait(lock) will call lock.unlock, which in turn calls mutex.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):

    #include <iostream>
    
    #define USE_BOOST 1
    
    #if USE_BOOST
    
    #include <boost/chrono.hpp>
    #include <boost/thread.hpp>
    #include <boost/thread/condition_variable.hpp>
    #include <boost/thread/locks.hpp>
    #include <boost/thread/recursive_mutex.hpp>
    namespace lib = boost;
    
    #else
    
    #include <chrono>
    #include <thread>
    #include <condition_variable>
    #include <mutex>
    namespace lib = std;
    
    #endif
    
    void bar();
    
    
    lib::recursive_mutex mutex;
    lib::condition_variable_any condvar;
    int value = 0;
    
    void foo()
    {
        std::cout << "foo()\n";
        lib::lock_guard<lib::recursive_mutex> lock(mutex);
        // Ownership level is now one
    
        bar();
    }
    
    void bar()
    {
        std::cout << "bar()\n";
        lib::unique_lock<lib::recursive_mutex> lock(mutex);
        // Ownership level is now two
    
        condvar.wait(lock); // Does this fully release the recursive mutex?
    
        std::cout << "value = " << value << "\n";
    }
    
    void notifier()
    {
        std::cout << "notifier()\n";
        lib::this_thread::sleep_for(lib::chrono::seconds(3));
        std::cout << "after sleep\n";
    
        // --- Program deadlocks here ---
        lib::lock_guard<lib::recursive_mutex> lock(mutex);
    
        value = 42;
        std::cout << "before notify_one\n";
        condvar.notify_one();
    }
    
    int main()
    {
        lib::thread t1(&foo); // This results in deadlock
        // lib::thread t1(&bar); // This doesn't result in deadlock
        lib::thread t2(&notifier);
        t1.join();
        t2.join();
    }
    

    I hope this helps anyone else facing the same dilemma when mixing condition_variable_any and recursive_mutex.