我在函数中有一个静态变量,但我希望它是基于每个线程静态的。
如何分配内存来创建我的C++类实例,以便每个线程都拥有自己的类实例的副本?
AnotherClass::threadSpecificAction()
{
// How to allocate this with thread local storage?
static MyClass *instance = new MyClass();
instance->doSomething();
}
这是在Linux上。我没有使用C++0x,而是gcc v3.4.6。