How to create static thread_local pointer on C++ in header-only lib?

问题描述 投票:0回答:0

我在 C++ sush 上的代码如下:(我编写了 concyrrency lib 并将 currentTask 存储在这个指针中,每个线程都可以找到他的任务——这是并发运行时中的常见解决方案)

// Task.hpp

class Task {
    void SomeMethod();
}

// Task.cpp

thread_local Task* pCurrentTask;

void Task::SomeMethod() {
    pCurrentTask = this;
}

现在我需要切换到 header-only lib。我怎样才能做到这一点?如何将此代码写入单个 hpp 文件?需要这只是所有翻译单元中的一个实例。有可能吗?

我的尝试:我在头文件库中找到全局变量但它对我不起作用

c++ multithreading concurrency header-files building
© www.soinside.com 2019 - 2024. All rights reserved.