如何使用非平凡构造函数构造静态 thread_local 成员
How to construct a static thread_local member with non-trivial constructor
我有一个 class 成员,我想构建它应该是访问它的每个线程的本地成员。虽然构造函数需要一些参数,所以我不能依赖静态零初始化。
class ThreadMem{
public:
ThreadMem(uint32 cachelineSize, uint32 cachelineCount);
};
class ThreadPool{
public:
ThreadPool(uint32 cachelineSize, uint32 cachelineCount){
// I need to prepare the `m_mem` in other threads with these arguments somehow
}
ThreadMem & mem() {
return m_mem;
}
private:
static thread_local ThreadMem m_mem;
};
哪里是构建 static thread_local ThreadMem ThreadPool::m_mem
的最佳位置,以便每个线程只构建一次,只有 ThreadPool
的构建线程可以在运行时计算值?
那个静态class成员是在main
进入之前的动态初始化阶段由C++ 运行-time构造的。其构造函数的参数必须在那时可用,这可能是不可行的。
我有一个 class 成员,我想构建它应该是访问它的每个线程的本地成员。虽然构造函数需要一些参数,所以我不能依赖静态零初始化。
class ThreadMem{
public:
ThreadMem(uint32 cachelineSize, uint32 cachelineCount);
};
class ThreadPool{
public:
ThreadPool(uint32 cachelineSize, uint32 cachelineCount){
// I need to prepare the `m_mem` in other threads with these arguments somehow
}
ThreadMem & mem() {
return m_mem;
}
private:
static thread_local ThreadMem m_mem;
};
哪里是构建 static thread_local ThreadMem ThreadPool::m_mem
的最佳位置,以便每个线程只构建一次,只有 ThreadPool
的构建线程可以在运行时计算值?
那个静态class成员是在main
进入之前的动态初始化阶段由C++ 运行-time构造的。其构造函数的参数必须在那时可用,这可能是不可行的。