单例模式

本文介绍了C++中两种常见的单例模式实现:懒汉模式和饿汉模式。懒汉模式在首次使用时实例化对象,饿汉模式则在类加载时就完成实例化。文中详细展示了两种模式的代码实现,并通过多线程示例展示了它们在并发环境下的应用。同时,讨论了它们各自适用的场景,懒汉模式适合访问量小的情况,而饿汉模式适用于访问量大且线程安全要求高的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单例模式:懒汉模式、恶汉模式
懒汉:第一次用到类实例的时候才会去实例化。(考虑线程安全性)
饿汉:单例类定义的时候就进行实例化。

适用场景
懒汉:在访问量较小时,采用懒汉实现。这是以时间换空间。
饿汉:由于要进行线程同步,所以在访问量比较大,或者可能访问的线程比较多时,采用饿汉实现,可以实现更好的性能。这是以空间换时间。

一、懒汉模式

#pragma once
#include <iostream> 
#include <mutex>  
#include <thread>
//初始化静态成员变量
SingleInstance* SingleInstance::m_SingleInstance = NULL;
std::mutex SingleInstance::m_Mutex;
///  加锁的懒汉式实现  //
class SingleInstance
{

public:
	// 获取单实例对象
	static SingleInstance*& GetInstance()
	{
		//  这里使用了两个 if判断语句的技术称为双检锁;好处是,只有判断指针为空的时候才加锁,
		//  避免每次调用 GetInstance的方法都加锁,锁的开销毕竟还是有点大的。
		if (m_SingleInstance == NULL)
		{
			//std::unique_lock<std::mutex> lock(m_Mutex); // 加锁
			m_Mutex.lock();
			if (m_SingleInstance == NULL)
			{
				m_SingleInstance = new (std::nothrow) SingleInstance;
			}
			m_Mutex.unlock();
		}
	
		return m_SingleInstance;
	}

	//释放单实例,进程退出时调用
	static void deleteInstance()
	{
		m_Mutex.lock();
		if (m_SingleInstance)
		{
			delete m_SingleInstance;
			m_SingleInstance = NULL;
		}
		m_Mutex.unlock();
	}

	// 打印实例地址
	void Print()
	{
		std::cout << "我的实例内存地址是:" << this << std::endl;
	}

private:
	// 将其构造和析构成为私有的, 禁止外部构造和析构
	SingleInstance()
	{
		std::cout << "构造函数" << std::endl;
	}
	~SingleInstance()
	{
		std::cout << "析构函数" << std::endl;
	}

	// 将其拷贝构造和赋值构造成为私有函数, 禁止外部拷贝和赋值
	SingleInstance(const SingleInstance& signal);
	const SingleInstance& operator=(const SingleInstance& signal);

private:
	// 唯一单实例对象指针
	static SingleInstance* m_SingleInstance;
	static std::mutex m_Mutex;
};

二、饿汉模式

#pragma once
#include <iostream> 
#include <thread>
//初始化静态成员变量
SingleInstance* SingleInstance::m_SingleInstance = new (std::nothrow) SingleInstance;//启动程序时直接实现
///  饿汉式实现  //
class SingleInstance
{

public:
	// 获取单实例对象
	static SingleInstance*& GetInstance()
	{
		return m_SingleInstance;
	}

	//释放单实例,进程退出时调用
	static void deleteInstance()
	{
		if (m_SingleInstance)
		{
			delete m_SingleInstance;
			m_SingleInstance = NULL;
		}
	}

	// 打印实例地址
	void Print()
	{
		std::cout << "我的实例内存地址是:" << this << std::endl;
	}

private:
	// 将其构造和析构成为私有的, 禁止外部构造和析构
	SingleInstance()
	{
		std::cout << "构造函数" << std::endl;
	}
	~SingleInstance()
	{
		std::cout << "析构函数" << std::endl;
	}

	// 将其拷贝构造和赋值构造成为私有函数, 禁止外部拷贝和赋值
	SingleInstance(const SingleInstance& signal);
	const SingleInstance& operator=(const SingleInstance& signal);

private:
	// 唯一单实例对象指针
	static SingleInstance* m_SingleInstance;
};
#include <thread> 
#include <iostream>
#include<windows.h>
#include "SingleInstance.h"

#define NUM_THREADS 5 // 线程个数

// 线程函数
void PrintHello(int threadid)
{

	std::cout << "Hi, 我是线程 ID:[" << threadid << "]" << std::endl;

	// 打印实例地址
	SingleInstance::GetInstance()->Print();
}

int main(void)
{
	int indexes[NUM_THREADS] = { 0 }; // 用数组来保存i的值

	int ret = 0;
	int i = 0;

	std::cout << "main() : 开始 ... " << std::endl;

	for (i = 0; i < NUM_THREADS; i++)
	{
		std::cout << "main() : 创建线程:[" << i << "]" << std::endl;

		indexes[i] = i; //先保存i的值
		
		std::thread thr(PrintHello, indexes[i]);
		thr.detach();
	}

	// 手动释放单实例的资源
	Sleep(10000);
	SingleInstance::deleteInstance();
	std::cout << "main() : 结束! " << std::endl;

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值