The std::barrier class is a new synchronization primitive introduced in C++20 that facilitates the synchronization of multiple threads working on a common task. It operates as a barrier that all threads must reach before the program can continue. Although the std::barrier object is similar to the std::mutex or std::condition_variable, there are some significant differences that make it unique.
How does std::barrier work?
Upon creating a std::barrier object, the number of participating threads must be specified. Each thread participating in the barrier uses the std::barrier::arrive_and_wait() function when it reaches the barrier. The std::barrier object monitors how many threads have arrived at the barrier. When all threads have reached the barrier, the barrier is lifted, and all threads can resume execution.
The std::barrier class sets itself apart from other synchronization primitives by allowing users to specify a callback function that executes after the barrier is lifted. This callback function can perform additional tasks that must be completed after the barrier has been crossed. This feature enhances the flexibility and versatility of the std::barrier class, making it a useful tool for multi-threaded programming in C++20.
Example:
C++
// C++ Program to demonstrate use of use std::barrier in
#include <barrier>
#include <iostream>
#include <thread>
std::barrier my_barrier{ 3 };
void my_function()
{
my_barrier.arrive_and_wait();
}
int main()
{
// creating threads
std::thread t1(my_function);
std::thread t2(my_function);
std::thread t3(my_function);
t1.join();
t2.join();
t3.join();
std::cout << "All threads have finished\n";
return 0;
}
Output:
All threads have finished
Explanation of the above program:
In this example, we demonstrate how to use the std::barrier class to synchronize multiple threads working on a common task. We create an instance of the std::barrier class, named my_barrier, with a count of 3. Three threads are created that call my_function(). At the barrier, each thread calls my_barrier.arrive_and_wait() to halt execution until all other threads have reached the barrier. Once all threads have arrived, the barrier is lifted, and all threads are released to resume execution. Upon finishing, the console displays the message "All threads have finished.".
Similar Reads
STD::array in C++ The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar
5 min read
std::forward in C++ In C++, std::forward() is a template function used for achieving perfect forwarding of arguments to functions so that it's lvalue or rvalue is preserved. It basically forwards the argument while preserving the value type of it.std::forward() was introduced in C++ 11 as the part of <utility> he
2 min read
std::distance in C++ The std::distance() in C++ STL is a built-in function used to calculate the number of elements between two iterators. It is defined inside <iterator> header file. In this article, we will learn about the std::distance function in C++ with examples.Example:C++// C++ Program to illustrate the us
5 min read
std::clamp in C++ 17 Clamps a variable to a given range[high - low]. If num > high, num is assigned high. If num < low, num is assigned low. If num is already clamped, no modifications. Note : This function is defined in header from C++17 onwards. Examples: Input : num = 100, Range : 10 - 90 Output : num = 90 Inpu
3 min read
attributes in C++ Attributes are one of the key features of modern C++ which allows the programmer to specify additional information to the compiler to enforce constraints(conditions), optimise certain pieces of code or do some specific code generation. In simple terms, an attribute acts as an annotation or a note to
11 min read
Modules in C++ 20 In C++20, the concept of modules was introduced to improve the efficiency of the compilation process and provide better organization and encapsulation of code. Modules allow developers to divide their code into separate components, each with its own interface and implementation, and import them as n
6 min read