Skip to content
Snippets Groups Projects
Commit 3c710f9b authored by MerryMage's avatar MerryMage
Browse files

Thread: Make Barrier reusable

parent 8c508334
No related branches found
No related tags found
No related merge requests found
......@@ -69,20 +69,19 @@ private:
class Barrier {
public:
explicit Barrier(size_t count_) : count(count_), waiting(0) {}
explicit Barrier(size_t count_) : count(count_), waiting(0), generation(0) {}
/// Blocks until all "count" threads have called Sync()
void Sync() {
std::unique_lock<std::mutex> lk(mutex);
// TODO: broken when next round of Sync()s
// is entered before all waiting threads return from the notify_all
const size_t current_generation = generation;
if (++waiting == count) {
generation++;
waiting = 0;
condvar.notify_all();
} else {
condvar.wait(lk, [&]{ return waiting == 0; });
condvar.wait(lk, [this, current_generation]{ return current_generation != generation; });
}
}
......@@ -91,6 +90,7 @@ private:
std::mutex mutex;
const size_t count;
size_t waiting;
size_t generation; // Incremented once each time the barrier is used
};
void SleepCurrentThread(int ms);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment