C++20 Concurrency-5: Barrier
In the previous article I discussed about std::latch
which is one of the two blocking synchronization primitives introduced in C++20 that can act on multiple threads. The second one being std::barrier
. Fundamentally, both needs to be initialized with a counter and can block multiple threads until the counter reaches zero. The only fundamental difference between std::latch
and std::barrier
is that the latter can be reused multiple times.
Once the counter reaches zero, and if the barrier object is required to be reused, one simply needs to wait a thread on the barrier causing it to restart the countdown to zero.
In this article, I’ll present a simple example of std::barrier
by means of game players plying a hypothetical card game and go into the details of the api of std::barrier
in another article.
1. Introduction to example
Before I present the example, lets see the setting of the hypothetical card game. I’m no card game geek but play card games regularly each with varied rules. Therefore I thought this would be a nice setting to explain std::barrier
. Lets call our game “The Barrier” to keep things simple and conserve the creativity of my neurons from discovering a new card game name for merely explaining C++ features.