4

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?
}
jotik
  • 17,044
  • 13
  • 58
  • 123
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
  • I can't find anything about mixing `condition_variable_any` and `recursive_mutex` in the Boost documentation. – Emile Cormier Aug 01 '12 at 03:39
  • Did you expect (or wished) the `wait` call to make the mutex unlocked? – curiousguy Aug 14 '12 at 21:02
  • @curiousguy : Yeah, I kinda did. That behavior would make it less likely to run into deadlock problems. – Emile Cormier Aug 14 '12 at 21:42
  • What about the calling function? Does it **expect** `bar()` to unlock the mutex? The issue here is the contract of `bar()` allowing it to mess with **other locks taken elsewhere**. If you want this is your designs, you need to make it very explicit. – curiousguy Aug 14 '12 at 21:49
  • @curiousguy, I'm not using `recursive_mutex` in any of my stuff. I just came across it while reading another question here, and wondered how it could interoperate with condition variables. – Emile Cormier Aug 15 '12 at 00:53
  • "_how it could interoperate with condition variables_" Badly! – curiousguy Aug 15 '12 at 01:43

2 Answers2

6

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.

Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
  • 1
    Care to explain the downvote? Is there a flaw in my reasoning? – Emile Cormier Aug 01 '12 at 04:36
  • http://meta.stackexchange.com/questions/2706/posting-and-answering-questions-you-have-already-found-the-answer-to – Emile Cormier Aug 01 '12 at 04:44
  • Not my downvote, but I support it, as the answer doesn't answer the real question, just claims that its unsolveable. The question should really be 'How can I use a condition_var with a recursive_mutex' – Chris Dodd Aug 01 '12 at 05:11
  • Mind blown. +1 for presumed intentional hilarity. – Brian Cain Aug 01 '12 at 05:12
  • Why is it that people think I'm joking when I'm dead serious? This issue was raised on the boost mailing list way back in 2004: http://lists.boost.org/Archives/boost/2004/09/72238.php I raised it again here in case the behavior of `condition_variable` has since been changed for `recursive_mutex`. It seems like a legitimate question to me. – Emile Cormier Aug 01 '12 at 05:19
  • @ChrisDodd: I'm not asking to solve a problem. I'm asking about the behavior of `condition_variable_any::wait` when a `recursive_mutex` is involved. I'll revise my question and answer accordingly. Thanks for the feedback. – Emile Cormier Aug 01 '12 at 05:23
  • 3
    +1 The C++11 specification is very similar to the boost documentation. This is a rare case of boost following the C++ draft (at the time) rather than vice-versa. `condition_variable_any` is proposed in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2447.htm and the specification at that time required the lock count of a recursive lock to be one on entry to wait. That specific wording has changed, but the intent hasn't. boost then subsequently picked it up. – Howard Hinnant Aug 01 '12 at 13:50
1

You can fix this design by adding a parameter allowed_unlock_count to every function which operates on the mutex object; there are two types of guarantees that can be made about allowed_unlock_count:

(permit-unlock-depth) allowed_unlock_count represents the depth of permitted unlocking of mutex: the caller allows bar to unlock the mutex allowed_unlock_count times. After such unlocking, no guarantee is made about the state of mutex.

(promise-unlock) allowed_unlock_count represents the depth of locking of mutex: the caller guarantees that unlocking mutex exactly allowed_unlock_count times will allow other threads to grab the mutex object.

These guarantees are pre- and post-conditions of functions.

Here bar depends on (promise-unlock):

// pre: mutex locking depth is allowed_unlock_count
void bar(int allowed_unlock_count)
{
    // mutex locking depth is allowed_unlock_count
    boost::unique_lock<boost::recursive_mutex> lock(mutex);
    // mutex locking depth is allowed_unlock_count+1

    // you might want to turn theses loops
    // into an a special lock object!
    for (int i=0; i<allowed_unlock_count; ++i)
        mutex.unlock();
    // mutex locking depth is 1

    condvar.wait(lock); // other threads can grab mutex

    // mutex locking depth is 1
    for (int i=0; i<allowed_unlock_count; ++i)
        mutex.lock();
    // mutex locking depth is allowed_unlock_count+1
}
// post: mutex locking depth is allowed_unlock_count

Called function must explicitly allowed to decrease locking depth by the caller.

curiousguy
  • 8,038
  • 2
  • 40
  • 58