Marco
Marco

Reputation: 7287

Check if the __thread keyword is available

I cannot find any function/macro that can tell me if the __thread keyword/feature exists.

For example I want to do something like this without the user defining HAS_TLS

#if HAS_TLS
    static __thread int mytlsdata;
#else
    static pthread_key_t mytlskey;
#endif

Upvotes: 3

Views: 306

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597875

TLS support using keywords is compiler-specific. LLVM/clang uses __thread, whereas VisualC++ uses __declspec(thread), C++Builder supports both __thread and __declspec(thread), etc. There are no pre--compiler macros to determine if TLS keywords are available, or what keywords they actually are. To write cross-platform code for TLS, you will have to detect specific compilers and code accordingly.

Upvotes: 1

zwol
zwol

Reputation: 140796

The only predefined, standard thing that even comes close is,

#if __STDC_VERSION__ >= 201112L && !defined __STDC_NO_THREADS__
    static _Thread_local int mytlsdata;
#endif

but this does not detect equivalent pre-C11 features, e.g. your __thread. And assuming that __STDC_VERSION__ accurately reflects a compiler's true capabilities has historically been ... unwise.

I would normally recommend Autoconf at this point, but it doesn't appear to have an off-the-shelf test to detect these, and if you've never done anything with Autoconf before, this is maybe not the place to start. Sorry I can't be more help.

Upvotes: 4

Related Questions