C++11中,如果一个静态的局部变量正在被初始化,那么并行程序将会等待静态变量初始化完成后再执行。
由于静态数据成员在类加载的时候执行其构造函数,因此我们将静态的类实例放在静态成员函数中,实现懒汉式单例模式。
class Singleton {
public:
~Singleton() {}
Singleton(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton& operator=(Singleton&&) = delete;
static Singleton& get();
private:
Singleton() {}
};
Singleton& Singleton::get() {
static Singleton instance;
return instance;
}