[cfe-dev] __thread support in Clang

Eric Christopher echristo at apple.com
Tue Mar 1 23:17:43 PST 2011


>> 
>> It depends on the OS, it isn't a clang specific feature.
> 
> Is it spcific to OS or binary format? I have following simple program
> 

It's specific to the OS (which generally has a specific binary format).

> __thread int a = 0;
> void main() {
>    a = 2;
> }
> 
> that is fails to compile with gcc-4.2 and clang-2.8 but compiles fine
> with gcc-4.5 from macports. I believe that gcc-4.5 has support of this
> feature on macosx. The binary produced by gcc-4.5 is Mach-O file.
> $ file ./a.out
> ./a.out: Mach-O 64-bit executable x86_64
> 

Yes, it uses an emulated tls that ends up being calls out to pthread
and semaphore functions. I don't believe there's any plans to implement
this sort of thing in clang - at the least I don't plan to :)

> I also googled following ticket that requests __thread "support on
> macosx" http://openradar.appspot.com/6850750 . It is closed as
> duplicated of an internal issue. The reporter claims "suggests that
> the specification of thread-local storage (tls) is ELF specific, it
> really isn't.  In fact the document referred to by the gcc page
> referenced in "SUMMARY:" describes the required implementation details
> and they are easily implemented on Darwin/Mac OS X without any need
> for ELF."
> 

Sure. It is possible to implement it in any format with the proper relocations.

>>> Are you going to
>>> implement it any time soon? If not - what is the best alternative of
>>> TLS on macosx/clang?
>> 
>> pthreads.
> 
> Yeah, there are pthread_getspecific/pthread_setspecific functions but
> they are less convenient than __thread.

Agreed.

-eric



More information about the cfe-dev mailing list