diff --git a/Common/Thread/Event.h b/Common/Thread/Event.h index 3baf465373..705d8efc1f 100644 --- a/Common/Thread/Event.h +++ b/Common/Thread/Event.h @@ -7,12 +7,15 @@ struct Event : public Waitable { public: + Event() { + triggered_ = false; + } + void Wait() override { - if (triggered_) { - return; + if (!triggered_) { + std::unique_lock lock; + cond_.wait(lock, [&] { return triggered_.load(); }); } - std::unique_lock lock; - cond_.wait(lock, [&] { return !triggered_; }); } void Notify() { @@ -24,5 +27,5 @@ public: private: std::condition_variable cond_; std::mutex mutex_; - bool triggered_ = false; + std::atomic triggered_; }; diff --git a/Common/Thread/Waitable.h b/Common/Thread/Waitable.h index 8a5de2e415..1f38bf33d8 100644 --- a/Common/Thread/Waitable.h +++ b/Common/Thread/Waitable.h @@ -14,7 +14,7 @@ public: void Wait() override { if (!triggered_) { std::unique_lock lock(mutex_); - cond_.wait(lock, [&] { return !triggered_; }); + cond_.wait(lock, [&] { return triggered_.load(); }); } } @@ -24,7 +24,7 @@ public: if (us == 0) return false; std::unique_lock lock(mutex_); - cond_.wait_for(lock, std::chrono::microseconds(us), [&] { return !triggered_; }); + cond_.wait_for(lock, std::chrono::microseconds(us), [&] { return triggered_.load(); }); } return triggered_; } diff --git a/unittest/TestThreadManager.cpp b/unittest/TestThreadManager.cpp index b327f02035..38dd6b2083 100644 --- a/unittest/TestThreadManager.cpp +++ b/unittest/TestThreadManager.cpp @@ -1,3 +1,5 @@ +#include + #include "Common/Log.h" #include "Common/TimeUtil.h" #include "Common/Thread/Barrier.h" @@ -62,7 +64,7 @@ bool TestParallelLoop(ThreadManager *threadMan) { // This is some ugly stuff but realistic. const size_t THREAD_COUNT = 6; // Must match the number of threads in TestMultithreadedScheduling -const size_t ITERATIONS = 1000; +const size_t ITERATIONS = 40000; static std::atomic g_atomicCounter; static ThreadManager *g_threadMan;