我想为非静态成员模拟线程局部变量,如下所示:
template< typename T, unsigned int tNumThread >
class ThreadLocal
{
private:
protected:
T mData[tNumThread];
unsigned int _getThreadIndex()
{
return ...; // i have a threadpool and each thread has an index from 0 to n
}
public:
ThreadLocal() {};
~ThreadLocal() {};
T& operator ->()
{
return mData[_getThreadIndex()];
}
...
};
但问题是线程数将在运行时确定,我必须mData
从堆中分配。
我想知道有没有办法不使用堆分配并使用上面的常规数组?