C++单例模式

单例模式是一种设计模式,它保证一个类只有一个对象。因此单例模式要私有化构造函数,禁用拷贝构造以及赋值重载。同时还要提供一个静态成员函数获取单例对象。

单例模式有两种实现方式:饿汉模式和懒汉模式

饿汉模式:创建静态单例对象,它在编译期间就被初始化,即在main函数执行之前就初始化

优点:线程安全,因为单例对象在main函数执行之前就初始化完成,不存在多线程竞争初始化单例对象的问题

缺点:占用资源,因为单例对象初始化时可能会加载许多其他资源,导致进程启动慢


懒汉模式:创建静态单例对象指针,实现延迟加载,只有在第一次请求单例对象时才初始化

优点:延迟初始化,避免加载不必要的资源

缺点:线程不安全,如果没有线程同步机制,可能会导致多个线程竞争初始化单例对象问题

1.饿汉模式

cpp 复制代码
//饿汉模式
class ConfigInfo
{
private:
	static ConfigInfo _info;//静态单例对象
	std::string _ip = "127.0.0.1";
	int _port = 8080;
private:
	//私有化构造函数
	ConfigInfo() {}
public:
	//禁用拷贝构造
	ConfigInfo(const ConfigInfo& info) = delete;
	//禁用赋值重载
	ConfigInfo& operator=(const ConfigInfo& info) = delete;
	//静态成员函数,返回单例对象指针
	static ConfigInfo* GetInstance()
	{
		return &_info;
	}
	//获取IP地址
	std::string GetIP()
	{
		return _ip;
	}
	//设置IP地址
	void SetIP(std::string ip)
	{
		_ip = ip;
	}
};
//静态单例对象的类外定义
ConfigInfo ConfigInfo::_info;
int main()
{
	ConfigInfo* info = ConfigInfo::GetInstance();
	std::cout << info->GetIP() << std::endl;
	info->SetIP("127.4.6.34");
	std::cout << info->GetIP() << std::endl;
	return 0;
}

2.懒汉模式

GetInstance函数中判断_info==nullptr会有线程安全问题:假设两个线程都成功进入了if内部,就会多次new分配资源,因此要进行加锁。加锁后又存在性能问题:

每个线程到此处都要进行加锁解锁,但实际上单例对象只有在第一次被调用时才会被判断为空并进行new资源分配,往后再被调用时一定不为nullptr。所以只要在得一次调用时进行加锁即可,这里要进行双检查加锁。

cpp 复制代码
//懒汉模式
class ConfigInfo
{
private:
	static ConfigInfo* _info;//静态单例对象
	std::string _ip = "127.0.0.1";
	int _port = 8080;
	static std::mutex _mtx;//静态锁
private:
	//私有化构造函数
	ConfigInfo() {}
public:
	//禁用拷贝构造
	ConfigInfo(const ConfigInfo& info) = delete;
	//禁用赋值重载
	ConfigInfo& operator=(const ConfigInfo& info) = delete;
	//静态成员函数,返回单例对象指针
	static ConfigInfo* GetInstance()
	{
		//此处判断_info==nullptr会有线程安全问题:假设两个线程都成功进入了if内部,就会多次new分配资源
		//因此要进行加锁
		//加锁后又存在性能问题:
		//每个线程到此处都要进行加锁解锁,但实际上单例对象只有在第一次被调用时才会被判断为空并进行new资源分配,往后再被调用时一定不为nullptr
		//所以只要在得一次调用时进行加锁即可
		//这里要进行双检查加锁
		if (_info == nullptr)
		{
			std::unique_lock<std::mutex> lock(_mtx);
			if (_info == nullptr)
			{
				_info = new ConfigInfo;
			}
		}
		return _info;
	}
	//获取IP地址
	std::string GetIP()
	{
		return _ip;
	}
	//设置IP地址
	void SetIP(std::string ip)
	{
		_ip = ip;
	}
};
//静态单例对象的类外定义
ConfigInfo* ConfigInfo::_info = nullptr;
//静态锁类外定义
std::mutex ConfigInfo::_mtx;
int main()
{
	ConfigInfo* info = ConfigInfo::GetInstance();
	std::cout << info->GetIP() << std::endl;
	info->SetIP("127.4.6.34");
	std::cout << info->GetIP() << std::endl;
	return 0;
}
相关推荐
blasit2 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_3 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星3 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛5 天前
delete又未完全delete
c++
端平入洛6 天前
auto有时不auto
c++
郑州光合科技余经理6 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1236 天前
matlab画图工具
开发语言·matlab
dustcell.6 天前
haproxy七层代理
java·开发语言·前端
norlan_jame6 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20216 天前
信号量和信号
linux·c++