#include <iostream>
#include <mutex>class Singleton {
public:
static Singleton* getInstance() {
if (!instance) { // 第一次检查
std::lock_guard<std::mutex> lock(mutex); // 加锁
if (!instance) { // 第二次检查
instance = new Singleton(); // 实际创建实例
}
}
return instance;
}void showMessage() {
std::cout << "Hello from Singleton!" << std::endl;
}private:
Singleton() {} // 私有构造函数
Singleton(const Singleton&) = delete; // 禁用拷贝构造函数
Singleton& operator=(const Singleton&) = delete; // 禁用赋值运算符static Singleton* instance; // 静态实例指针
static std::mutex mutex; // 互斥量
};// 初始化静态成员
Singleton* Singleton::instance = nullptr;
std::mutex Singleton::mutex;int main() {
Singleton::getInstance()->showMessage();
return 0;
}
以上为双重检测锁单例的代码示例,
首先原始的创建单例过程如下,判断instance为空时进行创建,创建后下次再调用getInstance方法时instance就不会空了,不会重复进行创建,保证了单例。
static Singleton* getInstance() {
if (!instance) {
instance = new Singleton(); // 实际创建实例
}
return instance;
}
但是以上代码在多线程中是不安全的,因为在第一次获取instance对象时,2个线程可能同时进行 instance是否为空的判断,此时他们都判断为空,则同时进入“ if (!instance) { }”判断语句的方法体中,此时单例模式的对象被创建两份已成为定数,并且在他们成功地“new Singleton(); ”之前,instance对象将一直为空,此时再有第三个线程进行“ if (!instance) { }”判断时,他仍然会进入方法体。
为了解决这一问题才会为“ if (!instance) { }”这一判断进行加锁,加锁后如下
static Singleton* getInstance() {
std::lock_guard<std::mutex> lock(mutex); // 加锁
if (!instance) {
instance = new Singleton(); // 实际创建实例
}
return instance;
}
嗯,多线程是安全了 ,但是在进行第一次创建实例之后的后续调用中,仅仅只是为了获取instance对象,但是却进行了无用处的加锁解锁(锁的获取和释放是比较耗时的操作),为避免不必要的加锁开销,我们再为他加上一层判断,这就是双重检测锁单例。
static Singleton* getInstance() {
if (!instance) { // 第一次检查
std::lock_guard<std::mutex> lock(mutex); // 加锁
if (!instance) { // 第二次检查
instance = new Singleton(); // 实际创建实例
}
}
return instance;
}