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

2021-04-05  本文已影响0人  less_sleep

 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

上一篇下一篇

猜你喜欢

热点阅读