C++设计模式1:单例模式(懒汉模式和饿汉模式,以及多线程问题处理)

饿汉单例模式

        程序还没有主动获取实例对象,该对象就产生了,也就是程序刚开始运行,这个对象就已经初始化了。 

class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		return &singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton singleton;
};
Singleton Singleton::singleton;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

        显然饿汉模式是线程安全的,因为单例对象的初始化发生在.bss段,和栈无关,而线程的启动依赖于函数,函数需要开辟栈内存,所以是线程安全的。但是饿汉模式也有缺点,如果这个单例类的构造函数过于复杂,包含了线程和数据库等等一系列的初始化过程,需要进行大量操作,就会导致程序启动变慢。

运行结果如下:    三个对象的地址是一样的,说明是同一个对象,并且最后也只是析构了一次。

 懒汉模式

实例对象直到程序中有模块获取它时,才会初始化这个对象。

#include<iostream>
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		if (singleton == nullptr)
		{
			singleton = new Singleton();
		}
		return singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
Singleton* Singleton::singleton=nullptr;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

运行结果。 

         上面这种写法显然是线程不安全的,因为要构造一个单例,构造函数里面可能需要进行大量的操作。这段代码就会产生竞态条件,我们需要通过线程间的互斥操作来解决。

#include<iostream>
#include<memory>
#include<thread>
#include<mutex>
std::mutex mtx;
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		std::lock_guard<std::mutex>loc(mtx);
		if (singleton == nullptr)
		{
			singleton = new Singleton();
		}
		return singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
Singleton* Singleton::singleton=nullptr;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

         这种写法虽然可以解决问题,但是加锁的位置,对程序的性能损耗较大,每次要先拿到锁才去判断是否为nullptr,如果不是,这把锁就白拿了,换一下加锁的位置。

        

#include<iostream>
#include<memory>
#include<thread>
#include<mutex>
std::mutex mtx;
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		if (singleton == nullptr)
		{
			std::lock_guard<std::mutex>loc(mtx);
			singleton = new Singleton();
		}
		return singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
Singleton* Singleton::singleton=nullptr;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

        这次加锁位置明显可以减少程序的性能损耗,但是会出现一个问题,假如开始单例是nullptr,一个线程通过if语句,并且拿到了锁,它只是开辟了内存,并且构造了单例对象,但是构造过程没有执行完全,还没有给这个单例对象赋值, 这时候这个单例还是nullptr,另一个线程这时候也可以通过if语句了,因为单例是nullptr,但是它不能构造单例,因为没有拿到锁,这时候第一个线程给单例赋值完成后,释放了锁,第二个线程拿到锁,就又构造了一次单例。

        要解决这个问题也简单,那就是双重if语句判断。

#include<iostream>
#include<memory>
#include<thread>
#include<mutex>
std::mutex mtx;
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		if (singleton == nullptr)
		{
			std::lock_guard<std::mutex>loc(mtx);
			if (singleton == nullptr)
			{
				singleton = new Singleton();
			}
		}
		return singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
Singleton* Singleton::singleton=nullptr;//类的静态成员变量要在类外定义
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

运行结果还是一样的。

        如果我们要简化上面的写法呢?我们可以使用到函数静态局部变量的初始化机制,函数静态局部变量在初始化的时候,底层的汇编指令会自动添加上线程互斥的指令,就可以省去我们加锁的步骤了。而且只有当程序主动调用get_instance函数的时候,单例才会被初始化,也省去了我们的nullptr双重判断了。

#include<iostream>
#include<memory>
#include<thread>
#include<mutex>
std::mutex mtx;
class Singleton
{
public:
	~Singleton()
	{
		std::cout << "~Singleton()" << std::endl;
	}
	static Singleton* get_instance()
	{
		static Singleton singleton;
		return &singleton;
	}
private:
	Singleton() {};
	Singleton(const Singleton& othersingle) = delete;
	Singleton& operator=(const Singleton& othersingle) = delete;
	static Singleton* singleton;
};
int main()
{
	Singleton* s1 = Singleton::get_instance();
	Singleton* s2 = Singleton::get_instance();
	Singleton* s3 = Singleton::get_instance();
	std::cout << "s1:" << s1 << std::endl;
	std::cout << "s2:" << s2 << std::endl;
	std::cout << "s3:" << s3 << std::endl;
}

运行效果一样。 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咩咩大主教

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值