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 6321233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:06:52+00:00 2026-05-24T16:06:52+00:00

Is there a mechanism to have a conditional variable use multiple mutexes? I am

  • 0

Is there a mechanism to have a conditional variable use multiple mutexes?
I am in Linux and pthreads in C++.

In an application, I need two mutexes (instead of one) to be atomically acquired and released by pthread_cond_wait(), but the function only accepts one.

I have a class named BlockingManager and it has the method:

blockMeFor( pthread_cond_t* my_cond, pthread_mutex_t* my_lock, set<int> waitees);

and I am using it, assuming that it acquires/releases the mutex just like pthread_cond_wait.

The problem is that for implementing blockingManager I need an internal mutex too and both of these mutexes should be acquired and released atomically.

There is a somehow related discussion here, it says waiting for more than one mutex yields undefined behavior.
http://sourceware.org/ml/libc-help/2011-04/msg00011.html

A producer/consumer model for the problem I am facing follows:

We have multiple clients.
Each client has some tasks.
Each task probably has multiple prerequisites (either among the tasks of the same client or other clients).
Each client has one consumer thread.
The tasks are assigned to the clients from one producer thread.
The newly assigned task may be eligible to be done before the previous tasks.
There may be no task to be done at some moments, but if there is a task to be done, at least one should be done. (It should be work-conserving)

I am using one condvar per consumer thread and it would block once there is no task to be done for that thread.
The condvar may be signaled by either

  • The producer thread assigning a new task.

  • Another consumer thread finishing a task.

I am using one mutex per consumer to protect the shared data-structures between producer & consumer.
And one mutex (internal mutex) to protect the shared data-structures between multiple-consumers.

  • 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-05-24T16:06:54+00:00Added an answer on May 24, 2026 at 4:06 pm

    In C++11 (if your compiler supports it) you can use std::lock to lock two mutexes at once (without deadlock). You can use this to build a Lock2 class which references two mutexes. And then you can use std::condition_variable_any to wait on a Lock2. This all might look something like:

    #include <mutex>
    #include <condition_variable>
    
    std::mutex m1;
    std::mutex m2;
    std::condition_variable_any cv;
    
    class Lock2
    {
        std::mutex& m1_;
        std::mutex& m2_;
    
    public:
        Lock2(std::mutex& m1, std::mutex& m2)
            : m1_(m1), m2_(m2)
        {
            lock();
        }
    
        ~Lock2() {unlock();}
    
        Lock2(const Lock2&) = delete;
        Lock2& operator=(const Lock2&) = delete;
    
        void lock() {std::lock(m1_, m2_);}
        void unlock() {m1_.unlock(); m2_.unlock();}
    };
    
    bool not_ready() {return false;}
    
    void test()
    {
        Lock2 lk(m1, m2);
        // m1 and m2 locked
        while (not_ready())
            cv.wait(lk);  // m1 and m2 unlocked
        // m1 and m2 locked
    }  // m1 and m2 unlocked
    

    If your compiler does not yet support these tools, you can find them in boost.

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

Sidebar

Related Questions

I have a web scraping application, written in OO Perl. There's single WWW::Mechanize object
Is there any mechanism in C# that allows one to define and declare an
Would like to ask for advice. there is a need for binary to have
Is there a mechanism or accepted approach for responding to requests that have a
Is there a mechanism to capture when a user touches on the navigation bar
Is there a mechanism where I can identify a string in an email, say
Is there a mechanism to dynamically load a GWT Module? The idea is to
Is there a standard mechanism or known library that will convert .png images to
Is there a built-in mechanism in .NET to match patterns other than regular expressions
Is there a built in mechanism for server push on Android? What I want

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.