Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8316537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:23:06+00:00 2026-06-08T21:23:06+00:00

When using condition_variable_any with a recursive_mutex , will the recursive_mutex be generally acquirable from

  • 0

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?
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:23:08+00:00Added an answer on June 8, 2026 at 9:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the timed_wait from boost C++ library and I am getting a
We are using an extractor application that will export data from the database to
In my ASP.Net page, I am loading data from server while scrolling using jQuery
I'm using a thread that is continuously reading from a queue. Something like: public
hi i stored a date that is taken from form in a variable using
I'm writing a performance-critical bidirectional streaming server using boost.asio. The server works this way
I have a pthread waiting on a condition variable using pthread_cond_wait() . It's waiting
Bjarne suggests using the condition in if's as scope restriction. In particular this example.
I am using this condition as part of my where clause: exists (select *
Suppose I let the user to write a condition using Javascript, the user can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.