To translate the program with maximum optimization, I have to use a volatile variable in the static method getInstance. I compile the program with maximum and without optimization. My Linux PC has four my Windows PC has two cores. Here I’m only interested in the performance. In contrast, the access from the multithreading program will be done by four threads. The first access will initialize the object. My strategyĪs a reference point for my performance measurement, I use a singleton object which I sequential access 40 million times. From a birds-eye, you can have guarantees from the C++ runtime, locks, or atomics. I’m totally curious about the performance implications. There are many different ways to initialize a singleton in C++11 in a thread-safe way. You get the performance numbers and can reason about your use cases for the thread-safe initialization of a variable. So in this post, I discuss different ways to initialize a singleton in a multithreading environment. From that point on, you can use it without synchronization. But the singleton pattern is an ideal use case for a variable, which can only be initialized in a thread-safe way. There are a lot of issues with the singleton pattern.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |