There are a lot of issues with the singleton pattern. I’m aware of that. But the singleton pattern is an ideal use case for a variable, which can only be initialized in a thread-safe way. From that point on, you can use it without synchronization. So in this post, I discuss different ways to initialize a singleton in a multithreading environment. You get the performance numbers and can reason about your use cases for the thread-safe initialization of a variable.
There are many different ways to initialize a singleton in C++11 in a thread-safe way. From a birds-eye, you can have guarantees from the C++ runtime, locks, or atomics. I’m totally curious about the performance implications.
As a reference point for my performance measurement, I use a singleton object which I sequential access 40 million times. The first access will initialize the object. In contrast, the access from the multithreading program will be done by four threads. Here I’m only interested in the performance. The program will run on two real PCs. My Linux PC has four; my Windows PC has two cores. I compile the program with maximum and without optimization. To translate the program with maximum optimization, I have to use a volatile variable in the static method getInstance. If not, the compiler will optimize away my access to the singleton, and my program becomes too fast.
I have three questions in my mind:
- What is the relative performance of the different singleton implementations?
- Is there a significant difference between Linux (GCC) and Windows (cl.exe)?
- What’s the difference between the optimized and non-optimized versions?
Finally, I collect all numbers in a table. The numbers are in seconds.
The reference values
The both compilers
The command line gives you the details of the compiler Here are the gcc and the cl.exe.
The reference code
At first, the single-threaded case. Of course, without synchronization.
I use in the reference implementation the so-called Meyers Singleton. The elegance of this implementation is that the singleton object instance in line 11 is a static variable with block scope. Therefore, instance will exactly be initialized when the static method getInstance (lines 10 – 14) will be executed the first time. In line 14, the volatile variable dummy is commented out. When I translate the program with maximum optimization, that has to change, so the call MySingleton::getInstance() will not be optimized away.
Now the raw numbers on Linux and Windows.
Guarantees of the C++ runtime
I already presented the details to the thread-safe initialization of variables in the post Thread-safe initialization of data.
The beauty of the Meyers Singleton in C++11 is that it’s automatically thread-safe. That is guaranteed by the standard: Static variables with block scope. The Meyers Singleton is a static variable with block scope, so we are done. It’s still left to rewrite the program for four threads.
I use the singleton object in the function getTime (lines 24 – 32). The function is executed by the four promise in lines 36 – 39. The results of the associate futures are summed up in line 41. That’s all. Only the execution time is missing.
The next step is the function std::call_once in combination with the flag std::once_flag.
The function std::call_once and the flag std::once_flag
You can use the function std::call_once to register a callable executed exactly once. The flag std::call_once in the following implementation guarantees that the singleton will be thread-safe initialized.
Here are the numbers.
Of course, the most obvious way is it protects the singleton with a lock.
The mutex wrapped in a lock guarantees that the singleton will be thread-safe initialized.
How fast is the classical thread-safe implementation of the singleton pattern?
Not so fast. Atomics should make a difference.
The handle to the singleton is atomic. Because I didn’t specify the C++ memory model, the default applies Sequential consistency.
Now I’m curious.
But we can do better. There is an additional optimization possibility.
The reading of the singleton (line 14) is an acquire operation, and the writing is a release operation (line 20). Because both operations occur on the same atomic I don’t need sequential consistency. The C++ standard guarantees that an acquire operation synchronizes with a release operation on the same atomic. These conditions hold in this case. Therefore, I can weaken the C++ memory model in lines 14 and 20. Acquire-release semantic is sufficient.
The acquire-release semantic has a similar performance as the sequential consistency. That’s not surprising because on x86, both memory models are very similar. We would get different numbers on an ARMv7 or PowerPC architecture. You can read the details on Jeff Preshing’s blog Preshing on Programming.
If I forget an import variant of the thread-safe singleton pattern, please let me know and send me the code. I will measure it and add the numbers to the comparison.
All numbers at one glance
Don’t take the numbers too seriously. I executed each program only once, and the executable is optimized for four cores on my two-core windows PC. But the numbers give a clear indication. The Meyers Singleton is the easiest to get and the fastest one. In particular, the lock-based implementation is by far the slowest one. The numbers are independent of the used platform.
But the numbers show more. Optimization counts. This statement is not totally accurate for the std::lock_guard based implementation of the singleton pattern.
I’m not so sure. This post is a translation of a german post I wrote half a year ago. My German post gets a lot of reaction. I’m not sure what will happen this time. A few days’ later, I’m sure. The next post will be about adding the elements of a vector. First, it takes in one thread.
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, and Bhavith C Achar.
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,