Here is a translation of the accepted answer to your first question to use C ++ 11 tools:
#include <mutex> #include <condition_variable> #include <thread> #include <stdio.h> class AutoResetEvent { public: explicit AutoResetEvent(bool initial = false); void Set(); void Reset(); bool WaitOne(); private: AutoResetEvent(const AutoResetEvent&); AutoResetEvent& operator=(const AutoResetEvent&); // non-copyable bool flag_; std::mutex protect_; std::condition_variable signal_; }; AutoResetEvent::AutoResetEvent(bool initial) : flag_(initial) { } void AutoResetEvent::Set() { std::lock_guard<std::mutex> _(protect_); flag_ = true; signal_.notify_one(); } void AutoResetEvent::Reset() { std::lock_guard<std::mutex> _(protect_); flag_ = false; } bool AutoResetEvent::WaitOne() { std::unique_lock<std::mutex> lk(protect_); while( !flag_ ) // prevent spurious wakeups from doing harm signal_.wait(lk); flag_ = false; // waiting resets the flag return true; } AutoResetEvent event; void otherthread() { event.WaitOne(); printf("Hello from other thread!\n"); } int main() { std::thread h(otherthread); printf("Hello from the first thread\n"); event.Set(); h.join(); }
Output:
Hello from the first thread Hello from other thread!
Update
The comments below tobsen note that AutoResetEvent
has the semantics of signal_.notify_all()
instead of signal_.notify_one()
. I did not change the code because the accepted answer to the first question used pthread_cond_signal
as opposed to pthread_cond_broadcast
, and I am arguing that this is a correct translation of this answer.
Howard hinnant
source share