Performance comparison of condition variables and atomics in C ++ 20
Source: Heise.de added 11th Jan 2021After introducing the std :: atomic_flag in my last article “Synchronization with atomic variables in C ++ 20” I would now like to delve deeper. Today I am implementing a ping-pong game with condition variables: std :: atomic_flag and std :: atomic . Let’s play the game!
The central question I want to answer in this article is this: Up which kind of threads can be synchronized fastest in C ++ 20? I use three different data types for this: std :: condition_variable , std :: atomic_flag and std :: atomic .
To get comparable numbers, I implement a ping-pong game. One thread executes a ping function and another a pong function . For the sake of simplicity, I’ll call the thread that performs the ping function ping thread and the other pong thread. The ping thread waits for the game to be notified by the pong thread. The pong thread in turn sends the notification back. The game should be played after 1. 000. 000 rallies are over. I run every game five times in order to get comparable performance figures.
I carried out the performance test with a current Visual Studio compiler, which already supports the synchronization with Atomics . In addition, the program is compiled with maximum optimization ( / Ox ).
The game starts with a conversion in C ++ 11.
Condition variables // pingPongConditionVariable.cpp # include
# include
#include
# include
bool dataReady {false};
std :: mutex mutex _;
std :: condition_variable condVar1; // (1 H: : condition_variable condVar2; // (2)
std :: atomic counter {};
constexpr int countlimit = 1 ‘000’ 000;
void ping () {
while (counter
brands: 11 AM other the game media: Heise.de
Related posts
Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88
Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88
Related Products
Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91
Warning: Invalid argument supplied for foreach() in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91