6
6
#include " multi_queue_d_ary_heap.h"
7
7
8
8
#include < atomic>
9
+ #include < barrier>
9
10
#include < thread>
10
11
#include < mutex>
11
12
#include < condition_variable>
@@ -47,7 +48,6 @@ class spin_lock_t {
47
48
* condition variable to coordinate thread synchronization.
48
49
*/
49
50
class barrier_mutex_t {
50
- // FIXME: Try std::barrier (since C++20) to replace this mutex barrier
51
51
std::mutex mutex_;
52
52
std::condition_variable cv_;
53
53
size_t count_;
@@ -60,17 +60,22 @@ class barrier_mutex_t {
60
60
* @param num_threads Number of threads that must call wait() before
61
61
* any thread is allowed to proceed
62
62
*/
63
- explicit barrier_mutex_t (size_t num_threads)
63
+ explicit inline barrier_mutex_t (size_t num_threads)
64
64
: count_(num_threads)
65
65
, max_count_(num_threads) {}
66
66
67
+ /* *
68
+ * Initialization method goes unused by this barrier implementation.
69
+ */
70
+ inline void init () {}
71
+
67
72
/* *
68
73
* @brief Blocks the calling thread until all threads have called wait()
69
74
*
70
75
* When the specified number of threads have called this method, all
71
76
* threads are unblocked and the barrier is reset for the next use.
72
77
*/
73
- void wait () {
78
+ inline void wait () {
74
79
std::unique_lock<std::mutex> lock{mutex_};
75
80
size_t gen = generation_;
76
81
if (--count_ == 0 ) {
@@ -110,13 +115,13 @@ class barrier_spin_t {
110
115
* @param num_threads Number of threads that must call wait() before
111
116
* any thread is allowed to proceed
112
117
*/
113
- explicit barrier_spin_t (size_t num_threads) { num_threads_ = num_threads; }
118
+ explicit inline barrier_spin_t (size_t num_threads) { num_threads_ = num_threads; }
114
119
115
120
/* *
116
121
* @brief Initializes the thread-local sense flag
117
122
* @note Should be called by each thread before first using the barrier.
118
123
*/
119
- void init () {
124
+ inline void init () {
120
125
local_sense_ = false ;
121
126
}
122
127
@@ -127,7 +132,7 @@ class barrier_spin_t {
127
132
* to arrive unblocks all waiting threads. This method avoids using locks or
128
133
* condition variables, making it potentially more efficient for short waits.
129
134
*/
130
- void wait () {
135
+ inline void wait () {
131
136
bool s = !local_sense_;
132
137
local_sense_ = s;
133
138
size_t num_arrivals = count_.fetch_add (1 ) + 1 ;
@@ -141,7 +146,41 @@ class barrier_spin_t {
141
146
}
142
147
};
143
148
144
- using barrier_t = barrier_spin_t ; // Using the spin-based thread barrier
149
+ /* *
150
+ * @brief Thread barrier implementation using std::barrier
151
+ *
152
+ * It ensures all participating threads reach a synchronization point
153
+ * before any are allowed to proceed further.
154
+ */
155
+ class standard_barrier_t {
156
+ // / @brief Internal barrier implementation.
157
+ std::barrier<> barrier_;
158
+
159
+ public:
160
+ /* *
161
+ * @brief Constructs a barrier for a specific number of threads
162
+ *
163
+ * @param num_threads
164
+ * Number of threads that must call wait() before any thread is allowed
165
+ * to proceed.
166
+ */
167
+ explicit inline standard_barrier_t (size_t num_threads)
168
+ : barrier_(num_threads) {}
169
+
170
+ /* *
171
+ * Initialization method goes unused by this barrier implementation.
172
+ */
173
+ inline void init () {}
174
+
175
+ /* *
176
+ * @brief Blocks the calling thread until all threads have called wait()
177
+ */
178
+ inline void wait () {
179
+ barrier_.arrive_and_wait ();
180
+ }
181
+ };
182
+
183
+ using barrier_t = standard_barrier_t ; // Using the standard thread barrier
145
184
146
185
/* *
147
186
* @class ParallelConnectionRouter implements the MultiQueue-based parallel connection
0 commit comments