aboutsummaryrefslogtreecommitdiff
path: root/src/common/thread.h
diff options
context:
space:
mode:
authorMerryMage <MerryMage@users.noreply.github.com>2016-04-14 12:54:06 +0100
committerMerryMage <MerryMage@users.noreply.github.com>2016-04-14 13:59:58 +0100
commit3c710f9b104acf218bce1054bd325f9a2515f8ca (patch)
treec94ba9cccdc9bb0e36d5ad3f95880e239762006a /src/common/thread.h
parent8c508334455629ed88bc49fd2c355d4af99f0c6a (diff)
Thread: Make Barrier reusable
Diffstat (limited to 'src/common/thread.h')
-rw-r--r--src/common/thread.h10
1 files changed, 5 insertions, 5 deletions
diff --git a/src/common/thread.h b/src/common/thread.h
index 9bd0e8e7ff..bbfa8befae 100644
--- a/src/common/thread.h
+++ b/src/common/thread.h
@@ -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);