-
Notifications
You must be signed in to change notification settings - Fork 15
/
barrier.h
44 lines (33 loc) · 993 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
39
40
41
42
43
44
#pragma once
#include <condition_variable>
#include <functional>
#include <mutex>
namespace faabric::util {
#define DEFAULT_BARRIER_TIMEOUT_MS 10000
class Barrier
{
public:
// WARNING: this barrier must be shared between threads using a shared
// pointer, otherwise there seems to be some nasty race conditions related
// to its destruction.
static std::shared_ptr<Barrier> create(
int count,
std::function<void()> completionFunctionIn,
int timeoutMs = DEFAULT_BARRIER_TIMEOUT_MS);
static std::shared_ptr<Barrier> create(
int count,
int timeoutMs = DEFAULT_BARRIER_TIMEOUT_MS);
explicit Barrier(int countIn,
std::function<void()> completionFunctionIn,
int timeoutMsIn);
void wait();
private:
int count = 0;
int visits = 0;
int currentPhase = 1;
std::function<void()> completionFunction;
int timeoutMs;
std::mutex mx;
std::condition_variable cv;
};
}