xcode c++ 线程本地存储 clang-503.0.40 (Mac OSX)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23791060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:56:40  来源:igfitidea点击:

c++ thread-local storage clang-503.0.40 (Mac OSX)

xcodemacosc++11clangthread-local-storage

提问by pier94

After I declared a variable in this way:

在我以这种方式声明一个变量之后:

   #include <thread>
   namespace thread_space
    {
    thread_local int s;
    } //etc.

i tried to compile my code using 'g++ -std=c++0x -pthread [sourcefile]'. I get the following error:

我尝试使用“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.

If i try to compile the same code on Linux with GCC 4.8.1 whit the same flags, i get a functioning executable file. I'm using clang-503.0.40 (the one which comes with Xcode 5.1.1) on a MacBook Pro running OSX 10.9.3. Can anybody explain me what i'm doing wrong? Thank you!!

如果我尝试在带有相同标志的 GCC 4.8.1 的 Linux 上编译相同的代码,我会得到一个正常运行的可执行文件。我在运行 OSX 10.9.3 的 MacBook Pro 上使用 clang-503.0.40(Xcode 5.1.1 附带的那个)。谁能解释一下我做错了什么?谢谢!!

采纳答案by Thomas

Try clang++ -stdlib=libc++ -std=c++11. OS X's outdated libstdc++ doesn't support TLS.

试试clang++ -stdlib=libc++ -std=c++11。OS X 过时的 libstdc++ 不支持 TLS。

Edit

编辑

Ok, this works for the normal clang version but not for the Xcode one.

好的,这适用于普通的 clang 版本,但不适用于 Xcode 版本。

I did a diff against Apple's clang (503.0.38)and the normal released oneand found the following difference:

我对Apple 的 clang (503.0.38)正常发布的做了一个比较,发现以下区别:

        .Case("cxx_thread_local",
-                 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported() &&
-                 !PP.getTargetInfo().getTriple().isOSDarwin())
+                 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())

So I think this is a bug in Apple's clang version (or they kept it in there on purpose - but still weird, because -vsays based on 3.4).

所以我认为这是 Apple 的 clang 版本中的一个错误(或者他们故意将它保留在那里 - 但仍然很奇怪,因为-v说基于 3.4)。

回答by glampert

Alternatively, you can use compiler extensions such as __thread(GCC/Clang) or __declspec(thread)(Visual Studio).

或者,您可以使用编译器扩展,例如__thread(GCC/Clang) 或__declspec(thread)(Visual Studio)。

Wrap it in a macro and you can easily port your code across different compilers and language versions:

将它包装在一个宏中,您可以轻松地将代码移植到不同的编译器和语言版本中:

#if HAS_CXX11_THREAD_LOCAL
    #define ATTRIBUTE_TLS thread_local
#elif defined (__GNUC__)
    #define ATTRIBUTE_TLS __thread
#elif defined (_MSC_VER)
    #define ATTRIBUTE_TLS __declspec(thread)
#else // !C++11 && !__GNUC__ && !_MSC_VER
    #error "Define a thread local storage qualifier for your compiler/platform!"
#endif

...

ATTRIBUTE_TLS static unsigned int tls_int;

回答by rsfinn

The clang compiler included in the Xcode 8 Beta and GM releases supports the C++11 thread_localkeyword with both -std=c++11and -std=c++14(as well as the GCC variants).

Xcode 8 Beta 和 GM 版本中包含的 clang 编译器支持 C++11thread_local关键字和-std=c++11-std=c++14(以及 GCC 变体)。

Earlier versions of Xcode apparently supported C-style thread local storage using the keywords __threador _Thread_local, according to the WWDC 2016 video "What's New in LLVM"(see the discussion beginning at 5:50).

根据WWDC 2016 视频“LLVM 中的新功能”(参见 5:50 开始的讨论),早期版本的 Xcode 显然支持使用关键字__threador 的C 样式线程本地存储。_Thread_local

回答by C. Scott Ananian

Seems like you might need to set the minimum OS X version you target to 10.7 or higher.

似乎您可能需要将目标的最低 OS X 版本设置为 10.7 或更高版本。