-
Notifications
You must be signed in to change notification settings - Fork 0
/
barrier.h
36 lines (31 loc) · 824 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
#pragma once
#include <atomic>
#include <mutex>
#include <condition_variable>
//commentaries:
// Consider a home computer.
// Let the number of threads doesn't exceed 256, so set type uint8_t.
// But the number of threads can be any. I chose the indexes according to the type uint8_t.
typedef uint8_t uint;
class IBarrier {
public:
virtual auto barrier() -> void const = 0;
virtual auto setNumTreads(uint numThreds) -> void const = 0;
IBarrier(uint numThreads);
protected:
uint numThreads;
};
class BarrierCond final : public IBarrier {
public:
BarrierCond();
auto setNumTreads(uint numThreds)-> void const override final;
auto barrier() -> void const override final;
private:
std::mutex mutex;
std::condition_variable conditionVariable;
enum State : bool
{
Direct, Forvard
} state;
size_t counter;
};