C++设计模式1:单例模式(懒汉模式和饿汉模式,以及多线程问题处理)

饿汉单例模式

程序还没有主动获取实例对象,该对象就产生了,也就是程序刚开始运行,这个对象就已经初始化了。

cpp 复制代码
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		return &singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton singleton;
};
Singleton Singleton::singleton;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

显然饿汉模式是线程安全的,因为单例对象的初始化发生在.bss段,和栈无关,而线程的启动依赖于函数,函数需要开辟栈内存,所以是线程安全的。但是饿汉模式也有缺点,如果这个单例类的构造函数过于复杂,包含了线程和数据库等等一系列的初始化过程,需要进行大量操作,就会导致程序启动变慢。

运行结果如下: 三个对象的地址是一样的,说明是同一个对象,并且最后也只是析构了一次。

懒汉模式

实例对象直到程序中有模块获取它时,才会初始化这个对象。

cpp 复制代码
#include<iostream>
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		if (singleton == nullptr)
		{
			singleton = new Singleton();
		}
		return singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
Singleton* Singleton::singleton=nullptr;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

运行结果。

上面这种写法显然是线程不安全的,因为要构造一个单例,构造函数里面可能需要进行大量的操作。这段代码就会产生竞态条件,我们需要通过线程间的互斥操作来解决。

cpp 复制代码
#include<iostream>
#include<memory>
#include<thread>
#include<mutex>
std::mutex mtx;
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		std::lock_guard<std::mutex>loc(mtx);
		if (singleton == nullptr)
		{
			singleton = new Singleton();
		}
		return singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
Singleton* Singleton::singleton=nullptr;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

这种写法虽然可以解决问题,但是加锁的位置,对程序的性能损耗较大,每次要先拿到锁才去判断是否为nullptr,如果不是,这把锁就白拿了,换一下加锁的位置。

cpp 复制代码
#include<iostream>
#include<memory>
#include<thread>
#include<mutex>
std::mutex mtx;
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		if (singleton == nullptr)
		{
			std::lock_guard<std::mutex>loc(mtx);
			singleton = new Singleton();
		}
		return singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
Singleton* Singleton::singleton=nullptr;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

这次加锁位置明显可以减少程序的性能损耗,但是会出现一个问题,假如开始单例是nullptr,一个线程通过if语句,并且拿到了锁,它只是开辟了内存,并且构造了单例对象,但是构造过程没有执行完全,还没有给这个单例对象赋值, 这时候这个单例还是nullptr,另一个线程这时候也可以通过if语句了,因为单例是nullptr,但是它不能构造单例,因为没有拿到锁,这时候第一个线程给单例赋值完成后,释放了锁,第二个线程拿到锁,就又构造了一次单例。

要解决这个问题也简单,那就是双重if语句判断。

cpp 复制代码
#include<iostream>
#include<memory>
#include<thread>
#include<mutex>
std::mutex mtx;
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		if (singleton == nullptr)
		{
			std::lock_guard<std::mutex>loc(mtx);
			if (singleton == nullptr)
			{
				singleton = new Singleton();
			}
		}
		return singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
Singleton* Singleton::singleton=nullptr;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

运行结果还是一样的。

如果我们要简化上面的写法呢?我们可以使用到函数静态局部变量的初始化机制,函数静态局部变量在初始化的时候,底层的汇编指令会自动添加上线程互斥的指令,就可以省去我们加锁的步骤了。而且只有当程序主动调用get_instance函数的时候,单例才会被初始化,也省去了我们的nullptr双重判断了。

cpp 复制代码
#include<iostream>
#include<memory>
#include<thread>
#include<mutex>
std::mutex mtx;
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		static Singleton singleton;
		return &singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

运行效果一样。

相关推荐
诺....34 分钟前
C语言不确定循环会影响输入输出缓冲区的刷新
c语言·数据结构·算法
老王熬夜敲代码42 分钟前
C++中的mutex、condition_val
c++·笔记·面试
闻缺陷则喜何志丹43 分钟前
【计算几何 二分查找】P12261 [蓝桥杯 2024 国 Java B] 激光炮|普及+
c++·数学·蓝桥杯·计算几何·洛谷
清轩轩1 小时前
UDS时间参数学习(应用层+网络层+会话层+传输层)
c语言·can·信息与通信·诊断·uds
Ivy_belief1 小时前
C++新特性汇总:涵盖C++11到C++23
java·c++·c++11·c++23
koddnty1 小时前
在c++中使用HOOK修改sleep函数
linux·c++
武藤一雄2 小时前
C# 万字拆解线程间通讯?
后端·微软·c#·.net·.netcore·多线程
誰能久伴不乏2 小时前
深入理解 `poll` 函数:详细解析与实际应用
linux·服务器·c语言·c++·unix
1024肥宅2 小时前
JavaScript常用设计模式完整指南
前端·javascript·设计模式
仰泳的熊猫3 小时前
1140 Look-and-say Sequence
数据结构·c++·算法·pat考试