C++11 并发与多线程(七、单例设计模式共享数据分析)

一、单例设计模式

class Test
{
public:
	static Test *GetInstance()
	{
		if (nullptr == ptrTest)
		{
			ptrTest = new Test();
			static Delete cl;	//定义一个静态释放对对象
		}
		return ptrTest;
	}
	class Delete	//类中嵌套来释放对象
	{
	public:
		~Delete()
		{
			if (Test::ptrTest)
			{
				delete Test::ptrTest;
				Test::ptrTest = nullptr;
			}
		}
	};

	void func()
	{
		cout << "测试类" << endl;
	}
	
private:
	Test() {};
	~Test()
	{
	};

private:
	static Test *ptrTest;
};
Test *Test::ptrTest = nullptr;

int main()
{
	Test::GetInstance()->func();
	cout << "I Love China" << endl;

    return 0;
}

二、单例设计模式共享数据分析

  1. 问题分析:需要我们自己创建线程(而不是主线程)中来创建 Test 这个单例类的对象,这种线程可能不止两个(最少两个),我们可能会面临GetInstance()这种成员函数需要互斥
    //线程入口函数
    void MyThread()
    {
    	cout << "我的线程开始了" << endl;
    	Test *ptrTest = Test::GetInstance();	//这里可能就有问题
    }
    int main()
    {
    	thread obj1(MyThread);
    	thread obj2(MyThread);
    	obj1.join();
    	obj2.join();
    
    	cout << "I Love China" << endl;
    
     return 0;
    }
    
  2. 解决办法:加双重锁定
if (nullptr == ptrTest)		//双重锁定(双重检查)
{
	unique_lock<mutex> unique(mutex1);
	if (nullptr == ptrTest)
	{
		ptrTest = new Test();
		static Delete cl;
	}
}

完整代码演示:

std::mutex mutex1;

class Test
{
public:
	static Test *GetInstance()
	{
		if (nullptr == ptrTest)		//双重锁定(双重检查)
		{
			unique_lock<mutex> unique(mutex1);
			if (nullptr == ptrTest)
			{
				ptrTest = new Test();
				static Delete cl;
			}
		}
		return ptrTest;
	}
	class Delete	//类中嵌套来释放对象
	{
	public:
		~Delete()
		{
			if (Test::ptrTest)
			{
				delete Test::ptrTest;
				Test::ptrTest = nullptr;
			}
		
		}
	};

	void func()
	{
		cout << "测试类" << endl;
	}
	
private:
	Test() {};
	~Test()
	{
	};

private:
	static Test *ptrTest;
};
Test *Test::ptrTest = nullptr;
//线程入口函数
void MyThread()
{
	cout << "我的线程开始了" << endl;
	Test *ptrTest = Test::GetInstance();	//这里可能就有问题
}
int main()
{
	thread obj1(MyThread);
	thread obj2(MyThread);
	obj1.join();
	obj2.join();

	cout << "I Love China" << endl;

    return 0;
}

三、std::call_once() :能够保证函数A()只被调用一次

  1. C++11 引入的函数,该函数的第二个参数是一个函数名,具备互斥量这种能力,效率上更高
  2. call_once() 需要与一个标记结合使用,这个标记 std::once_flag; 其实 once_flag 是一个结构,决定对应的函数A() 是否被执行,调用 call_once()成功后,就把这个标记设置为一种已调用状态,后续再次调用 call_once(), 只要once_flag 被设置为了 “已调用”状态,那么对应的函数A()就不会被执行了
    重要代码部分:
static void CreateInstance()
{
	ptrTest = new Test();
	static Delete cl;
}
static Test *GetInstance()
{
	std::call_once(g_flag, CreateInstance);		//两个线程同时执行到这里
	return ptrTest;
}

完整代码:

std::once_flag g_flag;	//定义一个系统的标记
class Test
{
public:
	static void CreateInstance()
	{
		ptrTest = new Test();
		static Delete cl;
	}
	static Test *GetInstance()
	{
		std::call_once(g_flag, CreateInstance);		//两个线程同时执行到这里
		return ptrTest;
	}
	class Delete	//类中嵌套来释放对象
	{
	public:
		~Delete()
		{
			if (Test::ptrTest)
			{
				delete Test::ptrTest;
				Test::ptrTest = nullptr;
			}
		
		}
	};

	void func()
	{
		cout << "测试类" << endl;
	}
	
private:
	Test() {};
	~Test()
	{
	};

private:
	static Test *ptrTest;
};
Test *Test::ptrTest = nullptr;
//线程入口函数
void MyThread()
{
	cout << "我的线程开始了" << endl;
	Test *ptrTest = Test::GetInstance();	//这里可能就有问题
}
int main()
{
	thread obj1(MyThread);
	thread obj2(MyThread);
	obj1.join();
	obj2.join();

	cout << "I Love China" << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
模式是一种设计模式,它保证类在整个程序中只能创建一个实。在多线程环境下,如果不加以处理,可能会出现多个线程同时调用getInstance()方法创建实的问题,从而违反了模式的原则。 为了在多线程环境下保证模式的正确性,可以采用以下几种解决方案: 1. 懒汉式-线程不安全:在 getInstance() 方法中进行实化时,没有进行多线并发控制,可能会导致创建多个实的问题。 2. 懒汉式-线程安全:在 getInstance() 方法加上 synchronized 关键字,使用同步锁来控制多线并发访问,确保只有一个线程能够创建实。但是,由于加锁会造成多线程竞争锁资源的性能损耗,因此并不推荐使用该方式。 3. 饿汉式:在类加载时就进行实化,保证了线程安全,不存在并发问题。但是,由于直接创建对象实,可能会占用空间,影响程序的性能。 4. 双重检查锁定:使用 volatile 关键字来保证多线程环境下的可见性,通过两次判断实是否为 null 来控制并发访问。第一次判断是为了避免不必要的同步锁开销,第二次判断是为了在实为 null 的情况下进行同步锁。这种方式可以避免懒汉式加锁方式的性能问题。 5. 静态内部类:利用类加载机制和类初始化锁的特性,在静态内部类中创建实,保证了线程安全性和延迟加载。通过静态内部类的方式创建,只有在调用 getInstance() 方法时才会加载内部类,从而实现了懒加载。 综上所述,针对多线程环境下的模式,可以根据具体需求选择适当的实现方式。在保证线程安全的前提下,尽量避免加锁操作,以提高程序的性能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值