Latches and Barriers
Latches and barriers are simple thread synchronisation mechanisms which enable some threads to block until a counter becomes zero. At first, don’t confuse the new barriers with memory barriers, also known as fences. In C++20 we get latches and barriers in two variations: std::latch
, and std::barrier
.
First, there are two questions:
- What are the differences between these two mechanisms to synchronise threads? You can use a
std::latch
only once, but you can use astd::barrier
more than once. Astd::latch
is useful for managing one task by multiple threads; astd::barrier
is useful for managing repeated tasks by multiple threads. Additionally, astd::barrier
enables you to execute a function in the so-called completion step. The completion step is the state when the counter becomes zero. - What use cases do latches and barriers support that cannot be done in C++11 and C++14 with futures, threads, or condition variables in combination with locks? Latches...