Fix misuses of cond.wait (should return true when you want to stop waiting!)

This commit is contained in:
Henrik Rydgård
2022-04-08 12:28:45 +02:00
parent 5b20ace502
commit bde54ccdc0
3 changed files with 13 additions and 8 deletions
+8 -5
View File
@@ -7,12 +7,15 @@
struct Event : public Waitable {
public:
Event() {
triggered_ = false;
}
void Wait() override {
if (triggered_) {
return;
if (!triggered_) {
std::unique_lock<std::mutex> lock;
cond_.wait(lock, [&] { return triggered_.load(); });
}
std::unique_lock<std::mutex> 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<bool> triggered_;
};
+2 -2
View File
@@ -14,7 +14,7 @@ public:
void Wait() override {
if (!triggered_) {
std::unique_lock<std::mutex> 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<std::mutex> 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_;
}
+3 -1
View File
@@ -1,3 +1,5 @@
#include <thread>
#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<int> g_atomicCounter;
static ThreadManager *g_threadMan;