I'm using the mutex and condition_variable pair to implement mult-threaded processing. I have read examples and solid explanations like this and that. However, I do not understand why separate variables trigger each other. For example,
mutex alert0, alert1;
condition_variable var0, var1;
void toy0() {
std::unique_lock<std::mutex> lock(alert0);
var0.wait(lock, [=] { return true; });
cout << "Toy0 triggered" << endl;
}
void toy1() {
std::unique_lock<std::mutex> lock(alert1);
var1.wait(lock, [=] { return true; });
cout << "Toy1 triggered" << endl;
}
void main(){
std::thread t0 = std::thread([=] {
toy0();
});
std::thread t1 = std::thread([=] {
toy1();
});
{
std::unique_lock<std::mutex> lock(alert0);
var0.notify_all();
}
t0.join();
t1.join();
return;
}
yields
Toy0 triggered
Toy1 triggered
If this is the intended outcome, how may I have cross-talk-free signals in different parts of the program so each wait() can be triggered by a specific condition_variable's notify_all(), not others'?