懒汉模式与饿汉模式
懒汉模式:获取单例时才初始化
线程不安全:
两个线程同时判断 instance
为空,创建了两个 instance
实例,不符合单例模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Singleton { private: static Singleton* instance; Singleton() {} public: static Singleton* getInstance() { if (instance == nullptr) { instance = new Singleton(); } return instance; } };
Singleton* Singleton::instance = nullptr;
|
解决方法:
添加锁,实例还未创建时枷锁,创建实例前需要再判断一次。可能一个线程已经在创建实例了,确保不会因为枷锁期间多个线程同时进入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <mutex>
class Singleton { private: static Singleton* instance; static std::mutex mtx; Singleton() {} public: static Singleton* getInstance() { if (instance == nullptr) { std::lock_guard<std::mutex> lock(mtx); if (instance == nullptr) { instance = new Singleton(); } } return instance; } };
Singleton* Singleton::instance = nullptr; std::mutex Singleton::mtx;
|
饿汉模式:在类一加载时就初始化了 instance
这个静态变量
(在编译时就可以被初始化), 不用判断是否存在,肯定已经存在,直接返回 instance
。
1 2 3 4 5 6 7 8 9 10 11
| class Singleton { private: static Singleton* instance; Singleton() {} public: static Singleton* getInstance() { return instance; } };
Singleton* Singleton::instance = new Singleton();
|