标签:线程安全 private class stat div static public style protect
1.懒汉模式。
class Singleton { private: static Singleton* m_instance; Singleton(){} public: static Singleton* getInstance(); }; Singleton* Singleton::getInstance() { if(NULL == m_instance) { Lock();//借用其它类来实现,如boost if(NULL == m_instance) { m_instance = new Singleton; } UnLock(); } return m_instance; }
2. 饿汉式:
class singleton { protected: singleton() {} private: static singleton* p; public: static singleton* initance(); }; singleton* singleton::p = new singleton; singleton* singleton::initance() { return p; }
饿汉式是线程安全的。
标签:线程安全 private class stat div static public style protect
原文地址:http://www.cnblogs.com/simplepaul/p/7857491.html