我的函数中有一个变量是静态的,但我希望它在每个线程的基础上是静态的.
如何为我的C++类分配内存,以便每个线程都有自己的类实例副本?
AnotherClass::threadSpecificAction()
{
// How to allocate this with thread local storage?
static MyClass *instance = new MyClass();
instance->doSomething();
}
Run Code Online (Sandbox Code Playgroud)
这是在Linux上.我没有使用C++ 0x,这是gcc v3.4.6.
我有一个dlopen()用于加载其他模块的应用程序.应用程序和模块使用gcc 4.6构建在Ubuntu 12.04 x86_64上,但是用于i386 arch.然后将二进制文件复制到具有完全相同操作系统的另一台计算机并正常工作.
但是,如果将它们复制到Ubuntu 12.04 i386,则某些(但不是全部)模块无法加载以下消息:
dlopen: cannot load any more object with static TLS
Run Code Online (Sandbox Code Playgroud)
我怀疑这是由__thread变量的使用引起的.但是,这些变量不会在加载的模块中使用 - 仅在加载器模块本身中使用.
有人可以提供任何其他信息,可能是什么原因?
我正在减少__thread变量的数量并优化它们(-ftls-model等等),我只是好奇为什么它不能在几乎相同的系统上工作.