After the introduction to
std::atomic_flag in my last post, Synchronization with Atomics in C++20, I want to dive deeper. Today, I created a ping-pong game using condition variables,
std::atomic<bool>. Let’s play.
The key question I want to answer in this post is: What is the fastest way to synchronize threads in C++20? I use in this post three different data types:
To get comparable numbers, I implement a ping-pong game. One thread executes a
ping function, and the other thread a
pong function. For simplicity reasons, I call the thread executing the
ping function the ping thread and the other thread the pong thread. The ping thread waits for the notification of the pong threads and returns the notification to the pong thread. The game stops after 1,000,000 ball changes. I perform each game five times to get comparable performance numbers.
I made my performance test with the brand new Visual Studio compiler because it already supports synchronization with atomics. Additionally, I compiled the examples with maximum optimization (
Let me start with C++11.
I use two condition variables in the program:
condVar2 (lines 1 and 2). The ping thread waits for the notification of
condVar1 and sends its notification with
dataReady protects against spurious and lost wakeups (see “C++ Core Guidelines: Be Aware of the Traps of Condition Variables“). The ping-pong game ends when
counter reaches the
nofication_one calls (lines 3) and the counter are thread-safe and are, therefore, outside the critical region.
Here are the numbers:
The average execution time is 0.52 seconds.
Porting this play to
std::atomic_flags‘s in C++20 is straightforward.
Here is the play using two atomic flags.
Two Atomic Flags
In the following program, I replace the waiting on the condition variable with the waiting on the atomic flag and the notification of the condition variable with the setting of the atomic flag followed by the notification.
condAtomicFlag1.wait(false) (1) blocks, if the value of the atomic flag is
false. On the contrary, it returns if
condAtomicFlag1 has the value
true. The boolean value serves as a kind of predicate and must, therefore, be set back to
false (2). Before the notification (3) is sent to the pong thread,
condAtomicFlag1 is set to
true (4). The initial setting of
true (5) starts the game.
std::atomic_flag the game end earlier.
On average, a game takes 0.32 seconds.
When you analyze the program, you may recognize that one atomics flag is sufficient for the play.
One Atomic Flag
Using one atomic flag makes the play easier to understand.
In this case, the ping thread blocks on
true but the pong thread blocks on
false. Using one or two atomic flags make no difference from the performance perspective.
The average execution time is 0.31 seconds.
I used in this example
std::atomic_flag such as an atomic boolean. Let’s give it another try with
From the readability perspective, I prefer the following C++20 implementation based on
std::atomic<bool> can internally use a locking mechanism such as a mutex. As I assumed, my Windows runtime is lock-free (1).
On average, the execution time is 0.38 seconds.
As expected, condition variables are the slowest way, and atomic flag is the fastest way to synchronize threads. The performance of a
std::atomic<bool> is in-between.
With C++20, we have a few new mechanisms for thread coordination. In my next post, I will look deeper into latches, barriers, and semaphores. They also allow it to play Ping-Pong.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,