Synchronizes an arbitrary number of threads.
More...
#include "nn/nlib/threading/Barrier.h"
Synchronizes an arbitrary number of threads.
- Description
- Set the number of threads (
N
) to be synchronized in the Init
function. Calling the Wait
function stops that thread when the number of threads reached is less than N
. All threads are awakened and execution is resumed when the number of ready threads reach N
. When this occurs, the counter is reset and the thread halts when it reaches the Wait
function.
- Sample code is provided below.
const int kNumThread = 4;
std::thread th_list[kNumThread];
nlib_ns::threading::Barrier barrier;
errno_t e = barrier.Init(kNumThread);
SUCCEED_IF(e == 0);
for (auto& th : th_list) {
th = std::thread([](void* p) {
}, &barrier);
}
for (auto& th : th_list) {
th.join();
}
Definition at line 33 of file Barrier.h.
◆ Init()
nn::nlib::threading::Barrier::Init |
( |
unsigned int |
count | ) |
|
|
inlinenoexcept |
Initializes a barrier.
- Parameters
-
[in] | count | Number of threads to wait for. |
- Return values
-
0 | No error occurred. |
EINVAL | Indicates that count is 0 . |
ENOMEM | Indicates that there are not enough system resources. |
Definition at line 43 of file Barrier.h.
◆ Wait()
nn::nlib::threading::Barrier::Wait |
( |
| ) |
|
|
inlinenoexcept |
Waits for a thread.
- Return values
-
0 | No error occurred. |
EBADF | Indicates that the Init function has not been called. |
ENOMEM | Indicates that there are not enough system resources. |
Definition at line 51 of file Barrier.h.
The documentation for this class was generated from the following files: