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

作者 : admin 本文共957个字,预计阅读时间需要3分钟 发布时间: 2024-06-10 共2人阅读

对于一个类,想要在每个线程种有且只有一个实例对象,且线程之间不共享该实例,可以按照单例模式的写法,同时使用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;
}

C++使用thread_local实现每个线程下的单例插图
可以看到,每个线程中构造了单独的A::instance

本站无任何商业行为
个人在线分享 » C++使用thread_local实现每个线程下的单例
E-->