-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExecBarrier.hpp
46 lines (38 loc) · 1 KB
/
ExecBarrier.hpp
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
39
40
41
42
43
44
45
/*
* vim:ts=4:shiftwidth=4:et:cindent:fileencoding=utf-8:
*/
#pragma once
#include <atomic>
#include <mutex>
#include <chrono>
#include <condition_variable>
class ExecBarrier
{
public:
std::atomic<size_t> mCnt;
const size_t mThreshold;
std::mutex mMutex;
std::condition_variable mCond;
ExecBarrier(const size_t aThreshold) : mCnt(0), mThreshold(aThreshold)
{
}
ExecBarrier(const ExecBarrier&) = delete;
ExecBarrier(ExecBarrier&&) = delete;
ExecBarrier& operator=(const ExecBarrier&) = delete;
ExecBarrier& operator=(ExecBarrier&&) = delete;
public:
void waitOnArrive(void)
{
mCnt++;
if (mCnt == mThreshold)
{
mCond.notify_all();
}
else
{
using namespace std::chrono_literals;
std::unique_lock sBarrierLock(mMutex);
mCond.wait(sBarrierLock);
}
}
};