-
Notifications
You must be signed in to change notification settings - Fork 0
/
Barrier.h
38 lines (30 loc) · 969 Bytes
/
Barrier.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
#include <mutex>
#include <stdint.h>
#include <condition_variable>
class ThreadBarrier {
public:
ThreadBarrier(const uint32_t &threadCount) :
_threadCount(threadCount),
_incomingThreads(0),
_threadsToGoOut(0) {}
void wait() {
std::unique_lock<std::mutex> lock(_mutex);
++_incomingThreads;
if (_incomingThreads == _threadCount) {
_incomingThreads = 0;
_threadsToGoOut = _threadCount - 1; // -1 since the current thread goes out immediately
_conditionalVar.notify_all();
} else {
_conditionalVar.wait(lock, [this]() {return this->_threadsToGoOut != 0; });
--_threadsToGoOut;
}
lock.unlock();
}
private:
std::mutex _mutex;
std::condition_variable _conditionalVar;
const uint32_t _threadCount;
uint32_t _incomingThreads;
uint32_t _threadsToGoOut;
};