Conclusion

This lesson concludes the performance measures of methods used in solving the thread-safe initialization of singleton problems in C++.

The numbers give a clear indication; the Meyers Singleton is the easiest to understand and the fastest one. It’s about two times faster than the atomic versions. As expected, the synchronization with the lock is the most heavyweight and, therefore, the slowest. In particular, std::call_once on Windows is a lot slower than on Linux.

Operating System (Compiler) Single Threaded Meyers Singleton std::lock_guard std::call_once Sequential Consistency Acquire-Release Semantic
Linux (GCC) 0.03 0.04 12.47 0.22 0.09 0.07
Windows (cl.exe) 0.02 0.03 15.48 1.74 0.07 0.07

I want to stress one point about the numbers explicitly: These are the summed up numbers for all four threads. That means that we get optimal concurrency with the Meyers Singleton because the Meyers Singleton is nearly as fast as the single threaded implementation.

Get hands-on with 1400+ tech skills courses.