C++20 Concurrency: std::barrier

A reusable alternative to std::latch

CMP
2 min readJun 20, 2023

I previously wrote about using std::latch to synchronize multiple threads. While that is a great mechanism for simple one-time synchronization scenarios where multiple threads need to meet at a synchronization point before continuing execution, it is less flexible than the other new C++20 concurrency primitive: std::barrier.

What is std::barrier?

Like std::latch, std::barrier is a synchronization mechanism that allows a fixed number of threads to synchronize at a certain point in their execution. However, there are a few key differences:

  • std::barrier is reusable: While std::latch can only be used one time (once the down-counter reaches 0, it becomes unusable), std::barrier can be reused multiple times. Once all threads have reached the sync point, the barrier resets for the next iteration.
  • std::barrier has a callback function: After the sync point is reached by all threads, the std::barrier executes a potentially empty callback function.
  • Slightly different “wait” behavior: std::latch uses count_down() to decrement its counter and wait() to block until the counter reaches 0, while std::barrier typically uses arrive_and_wait() to both decrement the counter and block. It is possible to do these steps independently (arrive() then wait() ) if desired.

Below is the example that I used previously for std::latch:

--

--

CMP

Software engineer specializing in operating systems, navigating the intracicies of the C++ language and systems programming.