c++11中线程安全单例模式 Meyers Singleton 和 call_once

 static Singleton ( Meyers Singleton)

class Singleton {

public:

static Singleton& Instance() {

  static Singleton theSingleton;

  return theSingleton;

}

/* more (non-static) functions here */

private:

Singleton(); // ctor hidden

Singleton(Singleton const&); // copy ctor hidden

Singleton& operator=(Singleton const&); // assign op. hidden

~Singleton(); // dtor hidden

};

2 call_once

Singleton* Singleton::m_instance;

Singleton* Singleton::getInstance() {

    static std::once_flag oc;//用于call_once的局部静态变量

    std::call_once(oc, [&] {  m_instance = new Singleton();});

    return m_instance;

}


https://blog.csdn.net/10km/article/details/49777749

你可能感兴趣的:(c++11中线程安全单例模式 Meyers Singleton 和 call_once)