C++使用thread_local实现每个线程下的单例

对于一个类,想要在每个线程种有且只有一个实例对象,且线程之间不共享该实例,可以按照单例模式的写法,同时使用C++11提供的thread_local关键字实现。

在单例模式的基础上,使用thread_local关键字修饰单例的instance,保证该静态成员在一个线程创建时创建单独的副本。

class A {
public:
	static std::shared_ptr<A> getInstance() {
		if (!instance) {
			std::lock_guard<std::mutex> lock(mutex);
			if (!instance) {
				instance = std::shared_ptr<A>(new A(), A::destory);
			}
		}
		return instance;
	}
	static void destory(A* ptr) {
		delete ptr;
	}
private:
	A() {
		std::cout << std::this_thread::get_id() << "构造" << std::endl;
	}
	~A() {
		std::cout << std::this_thread::get_id() << "析构" << std::endl;
	}
	A(const A&) = delete;
	A& operator=(const A&) = delete;
	thread_local static std::shared_ptr<A> instance;
	static std::mutex mutex;
};

std::mutex A::mutex;
thread_local std::shared_ptr<A> A::instance = nullptr;

由于使用了智能指针定义对象,智能指针不能自动调用private析构函数,因此需要自定义一个public函数destory(A* ptr)作为智能指针的删除器。

定义函数Func()作为主线程和子线程执行的函数,观察输出结果

void Func() {
	std::cout << std::this_thread::get_id() << "开始执行" << std::endl;
	A::getInstance();
}

int main()
{
	Func();
	auto t = std::thread(Func);
	t.join();
	return 0;
}

在这里插入图片描述
可以看到,每个线程中构造了单独的A::instance

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值