-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbarrier.h
More file actions
121 lines (102 loc) · 2.76 KB
/
barrier.h
File metadata and controls
121 lines (102 loc) · 2.76 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef BARRIER_H
#define BARRIER_H
#include <atomic>
#include <mutex>
#include <thread>
/**
* @brief The spinning_barrier class should provide a lock-free spinning
* barrier.
*
* Based on chill @ http://stackoverflow.com/a/8120707
*/
class spinning_barrier
{
public:
/**
* @brief spinning_barrier
* @param n number of threads to participate in the barrier. If 'n' is
* larger than the number of cores, consider using locking_barrier
* instead.
*/
spinning_barrier (unsigned int n) : n_ (n), nwait_ (0), step_(0), yield_(n > std::thread::hardware_concurrency()) {}
spinning_barrier (unsigned int n, bool yield) : n_ (n), nwait_ (0), step_(0), yield_(yield) {}
bool wait ()
{
unsigned int step = step_.load ();
if (nwait_.fetch_add (1) == n_ - 1)
{
// OK, last thread to come.
nwait_.store (0);
step_.fetch_add (1);
return true;
}
else if (yield_)
{
while (step_.load () == step)
std::this_thread::yield();
return false;
}
else
{
while (step_.load () == step)
;
return false;
}
}
private:
// Number of synchronized threads.
const unsigned int n_;
// Number of threads currently spinning.
std::atomic<unsigned int> nwait_;
// Number of barrier syncronizations completed so far, it's OK to wrap.
std::atomic<unsigned int> step_;
// Whether to yield or not
bool yield_;
public:
static void test();
};
/**
* @brief The locking_barrier class should provide a non-spinning barrier.
*/
class locking_barrier
{
public:
/**
* @brief locking_barrier
* @param n number of threads to participate in the barrier. If 'n' is
* smaller than or equal to the number of cores, consider using
* locking_barrier instead.
*/
locking_barrier (unsigned int n) : n_ (n), nwait_ (0), step_(0) {}
bool wait ()
{
std::unique_lock<std::mutex> l(m_);
unsigned int step = step_;
if (nwait_++ == n_ - 1)
{
// OK, last thread to come.
nwait_ = 0;
step_++;
cv_.notify_all ();
return true;
}
else
{
while (step_ == step)
cv_.wait (l);
return false;
}
}
private:
// Number of synchronized threads.
const unsigned int n_;
// Number of threads currently spinning.
unsigned int nwait_;
// Number of barrier syncronizations completed so far, it's OK to wrap.
unsigned int step_;
std::mutex m_;
std::condition_variable cv_;
public:
static void test();
};
#endif // BARRIER_H