C++ :单例模式

4 篇文章 0 订阅

 

1 教科书里的单例模式

我们都很清楚一个简单的单例模式该怎样去实现:构造函数声明为private或protect防止被外部函数实例化,内部保存一个private static的类指针保存唯一的实例,实例的动作由一个public的类方法代劳,该方法也返回单例类唯一的实例。

上代码: 

#pragma once
#include<iostream>


class Singleton_Test
{
private:
	Singleton_Test()
	{

	}
	~Singleton_Test()
	{

	}
	static Singleton_Test *m_Obj;

public:
	Singleton_Test* GetInstance();
	
};

Singleton_Test* Singleton_Test::m_Obj = NULL;


Singleton_Test* Singleton_Test::GetInstance()
{
	if (m_Obj == NULL)
	{
		m_Obj = new Singleton_Test();
		std::cout << "Creat the Singleton .." << std::endl;
	}
	std::cout << "Get the Singleton" << std::endl;
	return m_Obj;
}





 

#include<iostream>
#include"scoped_ptr_test.hpp"



int main()
{
	Singleton_Test *Mytest=NULL;
	Mytest->GetInstance();

	Singleton_Test *Test_02=NULL;
	Test_02->GetInstance();

	getchar();
	return 0;
}

 

 

2 懒汉与饿汉这是一个很棒的实现,简单易懂。但这是一个完美的实现吗?不!该方法是线程不安全的,考虑两个线程同时首次调用instance方法且同时检测到p是NULL值,则两个线程会同时构造一个实例给p,这是严重的错误!同时,这也不是单例的唯一实现!

单例大约有两种实现方法:懒汉与饿汉。

懒汉:故名思义,不到万不得已就不会去实例化类,也就是说在第一次用到类实例的时候才会去实例化,所以上边的经典方法被归为懒汉实现;

饿汉:饿了肯定要饥不择食。所以在单例类定义的时候就进行实例化。

特点与选择:

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

在访问量较小时,采用懒汉实现。这是以时间换空间。

3 线程安全的懒汉实现

线程不安全,怎么办呢?最直观的方法:加锁。

方法1:加锁的经典懒汉实现:

#pragma once
#include<iostream>
#include<boost/thread/mutex.hpp>
#include<boost/thread/thread.hpp>


/*
the point of ther
*/


class Singleton_Test
{
private:
	Singleton_Test()
	{

	}
	~Singleton_Test()
	{

	}
	static Singleton_Test *m_Obj;
	static boost::mutex m_mutex;
public:
	Singleton_Test* GetInstance();
	
};

Singleton_Test* Singleton_Test::m_Obj = NULL;
boost::mutex Singleton_Test::m_mutex;

Singleton_Test* Singleton_Test::GetInstance()
{
	boost::unique_lock<boost::mutex> lock(m_mutex);
	if (m_Obj == NULL)
	{
		m_Obj = new Singleton_Test();
		std::cout << "Creat the Singleton .." << std::endl;
	}
	std::cout << "Get the Singleton" << std::endl;
	return m_Obj;
}





 

#include<iostream>
#include"scoped_ptr_test.hpp"

void FunctionCreatSingleton()
{
	Singleton_Test *Test = NULL;
	Test->GetInstance();
}

int main()
{
	//Singleton_Test *Mytest=NULL;
	//Mytest->GetInstance();

	//Singleton_Test *Test_02=NULL;
	//Test_02->GetInstance();

	boost::thread *ThreadGroup[5];
	for (int i = 0;i < 5;i++)
	{
		ThreadGroup[i] = new boost::thread(&FunctionCreatSingleton);
	}

	getchar();
	return 0;
}

此方法也很容易实现,在instance函数里定义一个静态的实例,也可以保证拥有唯一实例,在返回时只需要返回其指针就可以了。推荐这种实现方法,真得非常简单。方法2:内部静态变量的懒汉实现

#pragma once
#include<iostream>
#include<boost/thread/mutex.hpp>
#include<boost/thread/thread.hpp>


/*
the point of ther
*/


class Singleton_Test
{
private:
	Singleton_Test()
	{

	}
	~Singleton_Test()
	{

	}
	static Singleton_Test *m_Obj;
	static boost::mutex m_mutex;
public:
	Singleton_Test* GetInstance();
	
};

//Singleton_Test* Singleton_Test::m_Obj =  new Singleton_Test();
Singleton_Test* Singleton_Test::m_Obj =NULL ;

boost::mutex Singleton_Test::m_mutex;

Singleton_Test* Singleton_Test::GetInstance()
{
	boost::unique_lock<boost::mutex> lock(m_mutex);
	if (m_Obj == NULL)
	{
		m_Obj = new Singleton_Test();
		std::cout << "Creat the sinleton .." << std::endl;
	}

	std::cout << "Get the sinleton .." << std::endl;
	return m_Obj;
}





为什么我不讲“线程安全的饿汉实现”?因为饿汉实现本来就是线程安全的,不用加锁。为啥?自己想!4 饿汉实现

#pragma once
#include<iostream>
#include<boost/thread/mutex.hpp>
#include<boost/thread/thread.hpp>


/*
the point of ther
*/


class Singleton_Test
{
private:
	Singleton_Test()
	{

	}
	~Singleton_Test()
	{

	}
	static Singleton_Test *m_Obj;
	static boost::mutex m_mutex;
public:
	Singleton_Test* GetInstance();
	
};

Singleton_Test* Singleton_Test::m_Obj =  new Singleton_Test();
boost::mutex Singleton_Test::m_mutex;

Singleton_Test* Singleton_Test::GetInstance()
{
	if (m_Obj == NULL)
	{
		std::cout << "Creat the sinleton .." << std::endl;
	}

	std::cout << "Get the sinleton .." << std::endl;
	return m_Obj;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值