Singleton done right in C++ 11

Today searching the internet I’ve found a “cool” and simple method to create a safe singleton that performs well in multithreading environment. With a small amount of lines you can achieve both: safe initialization and multithreading safety. class Singleton { static std::once_flag onceFlag; static Singleton* instance; public: static Singleton& getInstance(){ std::call_once(onceFlag, []{ instance = new Singleton(); }); return *instance; } private: Singleton(){}; }   As c++ documentation says the: The class std::once_flag is a helper structure for std::call_once. An object ...

Mai mult