[cfe-dev] __thread support in Clang

Anatol Pomozov anatol.pomozov at gmail.com
Tue Mar 1 23:07:10 PST 2011


Hi

On Tue, Mar 1, 2011 at 10:19 PM, Eric Christopher <echristo at apple.com> wrote:
>
> On Mar 1, 2011, at 10:11 PM, Anatol Pomozov wrote:
>
>> Hi,
>>
>> I am trying to port a program from Linux to macosx. The program uses
>> several features one of them is Thread-Local Storage. It uses __thread
>> GCC attribute for this.
>>
>> The program compiles fine on Linux i386 but fails on macosx
>> 10.6.4/clang2.8 with following error: "error: thread-local storage is
>> unsupported for the current target"
>>
>> My question is: what is the status of this feature?
>
> 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

__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

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."

>> 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.

>> This issue a little bit confusing as the LLVM2.0 release note [1]
>> states that "A number of ELF features are now supported by LLVM,
>> including ... Thread Local Storage (TLS) with the __thread keyword"
>
> OS X doesn't use ELF.



More information about the cfe-dev mailing list