#include #include #include #include #include "condition.hpp" std::mutex cout_mutex; struct TwoWait { TwoWait(ConditionPtr a, ConditionPtr b, const std::string& text) : conda(a), condb(b), msg(text) { } void operator()() { { std::lock_guard lock(cout_mutex); std::cout << "waiting for " << msg << std::endl; } ConditionSetPtr cset(new ConditionSet); cset->include(conda); cset->include(condb); std::mutex mutex; std::unique_lock lock(mutex); cset->wait(lock); { std::lock_guard lock(cout_mutex); std::cout << "returned from wait for " << msg << std::endl; } } ConditionPtr conda; ConditionPtr condb; std::string msg; }; int main() { auto a = std::make_shared(); auto b = std::make_shared(); auto c = std::make_shared(); std::thread t1(TwoWait(a, b, "a or b")); std::thread t2(TwoWait(b, c, "b or c")); std::thread t3(TwoWait(a, c, "a or c")); std::this_thread::sleep_for(std::chrono::milliseconds(10)); { std::lock_guard lock(cout_mutex); std::cout << "all threads started" << std::endl; std::cout << "signaling b: " << std::endl; b->notify_all(); } std::this_thread::sleep_for(std::chrono::milliseconds(10)); { std::lock_guard lock(cout_mutex); std::cout << "signaling c: " << std::endl; c->notify_all(); } std::this_thread::sleep_for(std::chrono::milliseconds(10)); t1.join(); t2.join(); t3.join(); }