C ++ stream-local storage clang-503.0.40 (Mac OSX)

After I declared the variable like this:

#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:

 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 with the same flags, I get an executable executable. I am using clang-503.0.40 (the one that comes with Xcode 5.1.1) on a MacBook Pro with OSX 10.9.3. Can someone explain to me what I'm doing wrong? Thank!

+15
c ++ 11 xcode clang thread-local-storage macos
May 21 '14 at 18:26
source share
4 answers

Try clang++ -stdlib=libc++ -std=c++11 . OS X deprecated libstdc ++ does not support TLS.

Edit

Well, this works for the normal version of clang, but not for Xcode.

I made the difference between Apple clang (503.0.38) and the normal release and found the following difference:

  .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 the version of Apple clang (or they saved it there on purpose, but still strange, because -v speaks based on 3.4).

+5
May 25 '14 at
source share

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

Wrap it with a macro, and you can easily port your code to 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; 
+3
Aug 19 '14 at 22:09
source share

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

Earlier versions of Xcode appeared to support local C-style thread storage using the __thread or _Thread_local , according to WWDC 2016's What's New in LLVM (see discussion starting at 5:50).

+2
Jun 14 '16 at 16:26
source share

It looks like you may need to install the minimum version of OS X that you are aiming for up to 10.7 or higher.

0
Oct 09 '15 at 10:44
source share



All Articles