Using more than one mutex with a conditional variable
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.
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.
精彩评论