C++设计模式(6)——单例模式

动机(Motivation)

  • 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性、以及良好的效率。

  • 如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例?

  • 这应该是类设计者的责任,而不是使用者的责任。

模式定义

保证一个类仅有一个实例,并提供一个该实例的全局访问点。 ——《设计模式》GoF

要点总结

  • Singleton模式中的实例构造器可以设置为protected以允许子类派生。

  • Singleton模式一般不要支持拷贝构造函数和Clone接口,因为这有可能导致多个对象实例,与Singleton模式的初衷违背。

  • 如何实现多线程环境下安全的Singleton?注意对双检查锁的正确实现。

非线程安全懒汉模式

//线程非安全版本
#include <iostream>

class Singleton 
{
private:
	Singleton()
	{
		std::cout << "Singleton" << std::endl;
	}
	Singleton(const Singleton& other);
	static Singleton* m_instance;
public:
	static Singleton* getInstance();
};

Singleton* Singleton::m_instance = nullptr;


Singleton* Singleton::getInstance() 
{
	if (m_instance == nullptr) 
	{
		m_instance = new Singleton();
	}
	return m_instance;
}

int main() 
{
	Singleton* instance = Singleton::getInstance();
	Singleton* instance_2 = Singleton::getInstance();
	return 0;
}

线程安全懒汉模式

#include <mutex>
#include <iostream>
#include <thread>

std::mutex mtx;

//线程安全版本,但锁的代价过高
class Singleton 
{
private:
	Singleton()
	{
		std::cout << "Singleton" << std::endl;
	}
	Singleton(const Singleton& other);
	static Singleton* m_instance;
public:
	static Singleton* getInstance();
};

Singleton* Singleton::m_instance = nullptr;

Singleton* Singleton::getInstance() 
{
	mtx.lock();
	if (m_instance == nullptr) 
	{
		m_instance = new Singleton();
	}
	mtx.unlock();
	return m_instance;
}

void func1()
{
	Singleton::getInstance();
}

void func2()
{
	Singleton::getInstance();
}

int main() 
{
	std::thread t1(func1);
	std::thread t2(func2);
	t1.join();
	t2.join();
	return 0;
}

双检查锁

#include <mutex>
#include <iostream>
#include <thread>

std::mutex mtx;

//双检查锁,但由于内存读写reorder不安全
class Singleton 
{
private:
	Singleton()
	{
		std::cout << "Singleton" << std::endl;
	}
	Singleton(const Singleton& other);
	static Singleton* m_instance;
public:
	static Singleton* getInstance();
};

Singleton* Singleton::m_instance = nullptr;


/*
reorder问题:
1、编译器读写分配内存正确的顺序是:先分配内存,再调用构造器,最后返回内存地址
2、编译器reorder优化有可能会改变顺序,先分配内存,再返回内存地址,最后调用构造器。
这样其他线程拿到的对象是没有构造函数的,故会出错
*/

Singleton* Singleton::getInstance() 
{

	if (m_instance == nullptr) 
	{
		mtx.lock();
		if (m_instance == nullptr) 
		{
			m_instance = new Singleton();
		}
		mtx.unlock();
	}
	return m_instance;
}

void func1()
{
	Singleton::getInstance();
}

void func2()
{
	Singleton::getInstance();
}

int main() 
{
	std::thread t1(func1);
	std::thread t2(func2);
	t1.join();
	t2.join();
	return 0;
}

C++11版本

#include <mutex>
#include <iostream>
#include <thread>
#include <atomic>


//C++ 11版本之后的跨平台实现 (volatile)

class Singleton
{
private:
	Singleton()
	{
		std::cout << "Singleton" << std::endl;
	}
	Singleton(const Singleton& other);
	static std::atomic<Singleton*> m_instance;//c++11
	static std::mutex m_mtx;
public:
	static Singleton* getInstance();

};

std::atomic<Singleton*> Singleton::m_instance;
std::mutex Singleton::m_mtx;

Singleton* Singleton::getInstance() 
{
	Singleton* tmp = m_instance.load(std::memory_order_relaxed);
	std::atomic_thread_fence(std::memory_order_acquire);//获取内存fence
	if (tmp == nullptr) 
	{
		std::lock_guard<std::mutex> lock(m_mtx);
		tmp = m_instance.load(std::memory_order_relaxed);
		if (tmp == nullptr) 
		{
			tmp = new Singleton;
			std::atomic_thread_fence(std::memory_order_release);//释放内存fence
			m_instance.store(tmp, std::memory_order_relaxed);
		}
	}
	return tmp;
}
void func1()
{
	Singleton::getInstance();
}

void func2()
{
	Singleton::getInstance();
}

int main() 
{
	std::thread t1(func1);
	std::thread t2(func2);
	t1.join();
	t2.join();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值