设计模式-单例模式

1.单例模式的定义

确保一个类只有一个实例,并且提供一个全局访问点来访问这唯一实例;

特点:

  • 一个类只有一个实例
  • 必须自己创建这个实例
  • 必须向整个系统提供这个实例

2 实例

2.1 创建方式:

  1. 将构造函数私有化,这样能保证只能在类内进行实例化;
  2. 实例对象设置为静态;
  3. 设置获取实例对象函数;

2.2具体实例

#include<iostream>
#include<string>
using namespace std;
class singleton
{
public:
	static singleton* getItself()
	{
		if (!itself)
		{
			std::cout << "开始创建" << endl;
			itself = new singleton();
		}
		return itself;//获取指针;
	}
private:
	singleton() {
		
	}//将构造函数私有化;
	static singleton*itself;//定义全局变量指针
};
singleton*singleton::itself = NULL;
int main()//测试代码;
{
	singleton*s1 = singleton:: getItself();

	singleton*s2 = singleton::getItself();
	return 0;
}

3多线程下的安全问题

3.1 多线程下创建线程

采用多线程形式创建单例模式:

#include<iostream>
#include<string>
#include <process.h>
#include <Windows.h>
#define THREAD_NUM 5
using namespace std;
class singleton
{
public:
	static singleton* getItself()
	{
		if (!itself)
		{
			std::cout << "开始创建" << endl;
			itself = new singleton();
		}
		return itself;

	}
private:
	singleton() {
		
	}
	static singleton*itself;
};
singleton*singleton::itself = NULL;
/*非线程安全 单例模式*/


unsigned int __stdcall CallSingleton(void *pPM)
{
	singleton *s = singleton::getItself();
	int nThreadNum = *(int *)pPM;
	Sleep(50);
	printf("线程编号为%d\n", nThreadNum);
	return 0;
}

int main()
{
	HANDLE  handle[THREAD_NUM];

	//线程编号
	int threadNum = 0;
	while (threadNum < THREAD_NUM)
	{
		handle[threadNum] = (HANDLE)_beginthreadex(NULL, 0, CallSingleton, &threadNum, 0, NULL);
		//等子线程接收到参数时主线程可能改变了这个i的值
		threadNum++;
	}
	//保证子线程已全部运行结束
	WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);

	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
发现有多个实例被创建,所以其不是线程安全的;

3.2 添加锁;

#include<mutex>
#define THREAD_NUM 5
using namespace std;
class singleton
{
public:
	static singleton* getItself()
	{
		m_lock.lock();
		if (!itself)
		{
			std::cout << "开始创建" << endl;
			itself = new singleton();
		}
		return itself;
		m_lock.unlock();
	}
private:
	static std::mutex m_lock;
	singleton() {
		
	}
	static singleton*itself;
};
singleton*singleton::itself = NULL;

std::mutex singleton::m_lock;



运行结果:
在这里插入图片描述

4懒汉模式和饿汉模式

4.1 懒汉模式

定义:当程序需要创建该单例实例时,才会去进行创建;
参考:懒汉模式

4.2 懒汉模式(加锁模式)

class singleton
{
public:
	static singleton* getItself()
	{
		m_lock.lock();
		if (!itself)
		{
			std::cout << "开始创建" << endl;
			itself = new singleton();
		}
		return itself;
		m_lock.unlock();
	}
private:
	static std::mutex m_lock;
	singleton() {
		
	}
	static singleton*itself;
};
singleton*singleton::itself = NULL;

std::mutex singleton::m_lock;

4.3 懒汉模式,智能指针+互斥锁

#include <iostream>
#include <memory> // shared_ptr
#include <mutex>  // mutex
#include<thread>
using namespace std;
class Singleton {

public:
	typedef std::shared_ptr<Singleton> Ptr;  //智能指针可以释放聂村
	~Singleton() {
		cout << "析构函数" << endl;
	}

	static Ptr getInstance() {
		if (_instance == nullptr) { //双检锁
			//加锁
			std::lock_guard<std::mutex> lk(_mutex);

			_instance = std::shared_ptr<Singleton>(new Singleton);
		}
		return _instance;
	}

private:
	//构造函数
	Singleton();

	//复制构造函数 和 赋值构造函数
	Singleton(const Singleton &sigle);
	const Singleton &operator=(const Singleton &sigle);

	static Ptr _instance;
	static std::mutex _mutex;
};

Singleton::Ptr Singleton::_instance = nullptr;
std::mutex Singleton::_mutex;
Singleton::Singleton::Singleton() {
	cout << "构造函数" << endl;
}

智能指针用于保证其内存安全,互斥锁用于保证其线程安全;

测试代码:

void proc()
{
	cout << "我是次线程" << endl;
	Singleton::Ptr p = Singleton::getInstance();
	cout << "&p1 = " << p << endl;
}
int main() {

	cout << "我是主线程" << endl;

	thread th2(proc);//第一个参数为函数名,第二个参数为该函数的第一个参数,如果该函数接收多个参数就依次写在后面。此时线程开始执行。
	cout << "主线程中显示子线程id为" << th2.get_id() << endl;
	thread th3(proc);
	cout << "主线程中显示子线程id为" << th3.get_id() << endl;
	th3.join();//此时主线程被阻塞直至子线程执行结束。

	th2.join();//此时主线程被阻塞直至子线程执行结束。


	
	

	return 0;

}

运行结果:
在这里插入图片描述

4.4 懒汉模式,基于C++11 特性,先定义全局变量

#include <iostream>
#include <memory> // shared_ptr
#include <mutex>  // mutex
#include<thread>
using namespace std;
class Singleton {

public:
	static Singleton&GetIt()
	{
	   static Singleton itself;
	   return itself;
	}

private:
	Singleton() {
		std::cout << "构造函数" << std::endl;
	}
	~Singleton() {
		std::cout << "析构函数" << std::endl;
	}

	//复制构造函数 和 赋值构造函数
	Singleton(const Singleton &sigle);
	const Singleton &operator=(const Singleton &sigle);

	
	
};





void proc()//线程函数
{
	cout << "我是次线程" << endl;
	Singleton& p = Singleton::GetIt();
	cout << &p << endl;
}
int main() {

	cout << "我是主线程" << endl;
	int a = 9;
	thread th2(proc);//第一个参数为函数名,第二个参数为该函数的第一个参数,如果该函数接收多个参数就依次写在后面。此时线程开始执行。
	cout << "主线程中显示子线程id为" << th2.get_id() << endl;
	thread th3(proc);
	cout << "主线程中显示子线程id为" << th3.get_id() << endl;
	th3.join();//此时主线程被阻塞直至子线程执行结束。

	th2.join();//此时主线程被阻塞直至子线程执行结束。


	
	

	return 0;

}

在这里插入图片描述
原理:如果当变量在初始化的时候,并发同时进入声明语句,并发线程将会阻塞等待初始化结束。

4.5 饿汉模式

进程一开始就被创建,所以不存在线程安全;

#include <iostream>
#include <memory> // shared_ptr
#include <mutex>  // mutex
#include<thread>
using namespace std;
class Singleton {

public:
	static Singleton*getIt()
	{
		return instance;
	}

private:
	Singleton() {
		std::cout << "构造函数" << std::endl;
	}
	~Singleton() {
		std::cout << "析构函数" << std::endl;
	}

	//复制构造函数 和 赋值构造函数
	Singleton(const Singleton &sigle);
	const Singleton &operator=(const Singleton &sigle);

	static Singleton*instance;
	
};

Singleton*Singleton::instance = new Singleton();





void proc()
{
	cout << "我是次线程" << endl;
	Singleton*p = Singleton::getIt();
	cout << p << endl;
}
int main() {

	cout << "我是主线程" << endl;
	int a = 9;
	thread th2(proc);//第一个参数为函数名,第二个参数为该函数的第一个参数,如果该函数接收多个参数就依次写在后面。此时线程开始执行。
	cout << "主线程中显示子线程id为" << th2.get_id() << endl;
	thread th3(proc);
	cout << "主线程中显示子线程id为" << th3.get_id() << endl;
	th3.join();//此时主线程被阻塞直至子线程执行结束。

	th2.join();//此时主线程被阻塞直至子线程执行结束。


	
	

	return 0;

}

在这里插入图片描述
注:这里显示构造函数在主线程之前,说明进程一开始就把线程创建了;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值