我的函数中有一个变量是静态的,但我希望它在每个线程的基础上是静态的.
如何为我的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.
在我以这种方式声明变量之后:
#include <thread>
namespace thread_space
{
thread_local int s;
} //etc.
Run Code Online (Sandbox Code Playgroud)
我试图使用'g ++ -std = c ++ 0x -pthread [sourcefile]'编译我的代码.我收到以下错误:
example.C:6:8: error: thread-local storage is unsupported for the current target
static thread_local int s;
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用GCC 4.8.1在Linux上使用相同的标志编译相同的代码,我会得到一个正常运行的可执行文件.我在运行OSX 10.9.3的MacBook Pro上使用了clang-503.0.40(Xcode 5.1.1附带的那个).谁能解释一下我做错了什么?谢谢!!
我读到C++中有一个新关键字:它__thread来自我读过的内容.
我所知道的是,它是一个像static关键字一样使用的关键字,但我什么都不知道.这个关键字是否仅仅意味着,例如,如果变量被声明为:
__thread int foo;
Run Code Online (Sandbox Code Playgroud)
然后用新线程执行与该变量有关的任何事情?
我目前正在尝试在虚幻引擎项目中使用 assimp 库http://assimp.org 。我能够编译该库的 Windows 和 Mac 版本,以便我可以将它们链接到我的项目。
我现在面临的问题是编译iOS版本。正如我在项目中提交的 github 问题中所述,在编译时我不断遇到以下问题。https://github.com/assimp/assimp/issues/3690
我不认为这是它本身的问题assimp。我相信这是 macOS 及其编译 C++ 代码方式的问题。
这是我提交的 github 问题。我相信它包含解释我遇到的错误所需的所有信息。我只是不确定此时如何解决它。
在 macOS 上,我已从 cmake --build cd 构建了 assimp 到 port/iOS/ ./build.sh 中,但出现以下错误。
[ 92%] Building CXX object code/CMakeFiles/assimp.dir/AssetLib/Assjson/json_exporter.cpp.o
In file included from ~/assimp/code/Pbrt/PbrtExporter.cpp:87:
~/assimp/code/Pbrt/stb_image.h:931:1: error: thread-local storage is not supported for the
current target
STBI_THREAD_LOCAL
^
~/assimp/code/Pbrt/stb_image.h:592:39: note: expanded from macro 'STBI_THREAD_LOCAL'
#define STBI_THREAD_LOCAL thread_local
^
~/assimp/code/Pbrt/stb_image.h:1070:8: error: thread-local storage is not supported for the
current …Run Code Online (Sandbox Code Playgroud) 如本回答所示,thread_local即使设置了C++ 11标志,Mac OSX上Xcode的clang也不支持存储.即使在最新版本上,Apple LLVM版本7.0.0(clang-700.1.76),目标:x86_64-apple-darwin15.0.0,线程模型:posix,不支持thread_local:
../../src/dir/sysArch.h:1505:3: error: thread-local storage is not supported
for the current target
thread_local
^
Run Code Online (Sandbox Code Playgroud) c++ ×4
c++11 ×3
clang ×2
xcode ×2
assimp ×1
ios ×1
linux ×1
macos ×1
new-operator ×1
thread-local ×1