Well, i'm using singleton in my own project. The most frequently used singleton implementation might be Meyer's singletion and that of using std::call_once or pthread_once. Both of them are thread safe for parallelized computing
//Meyer's singleton
class MySingleton{
public:
static MySingleton& getInstance(){
static MySingleton instance;
// volatile int dummy{};
return instance;
}
private:
MySingleton()= default;
~MySingleton()= default;
MySingleton(const MySingleton&)= delete;
MySingleton& operator=(const MySingleton&)= delete;
};
//with call_once
class MySingleton{
public:
static MySingleton& getInstance(){
std::call_once(initInstanceFlag, &MySingleton::initSingleton);
// volatile int dummy{};
return *instance;
}
private:
MySingleton()= default;
~MySingleton()= default;
MySingleton(const MySingleton&)= delete;
MySingleton& operator=(const MySingleton&)= delete;
static MySingleton* instance;
static std::once_flag initInstanceFlag;
static void initSingleton(){
instance= new MySingleton;
}
};
MySingleton* MySingleton::instance= nullptr;
std::once_flag MySingleton::initInstanceFlag;
Meyer's implimentation using local static variable to ensure thread safety and return the instance identity, while the latter one achieved by call_once and reture a pointer. In my experiment, the Meyer's implimentation sames a little bit faster. But most project using the call_once approach for the most basic singleton implimentation, althought some small component of the project using Meyer's implimentation. I just wondering is there any principle to follow, and what's the pros and cons of this two different implimentation?