C11 introduces the _Thread_local
storage class specifier that can be used in combination with the static
and extern
storage class specifiers to declare a variable as thread local. The GNU C compiler suite implements a storage class specifier __thread
with the same same semantics.
Unfortunately I did not find any compiler (I tried gcc, clang and SUN studio) that actually implements the _Thread_local
keywords. I currently use the following construct to declare a keyword thread_local
:
/* gcc doesn't know _Thread_local from C11 yet */
#ifdef __GNUC__
# define thread_local __thread
#elif __STDC_VERSION__ >= 201112L
# define thread_local _Thread_local
#else
# error Don't know how to define thread_local
#endif
I know that this probably does not work with MSVC and other compilers. Can anybody suggest me a better method to declare thread_local
in a way that it works in as many compilers as possible?
Edit
Christoph suggested that Microsoft Visual C allows __declspec(thread)
. This is the updated macro definition:
/* gcc doesn't know _Thread_local from C11 yet */
#ifdef __GNUC__
# define thread_local __thread
#elif __STDC_VERSION__ >= 201112L
# define thread_local _Thread_local
#elif defined(_MSC_VER)
# define thread_local __declspec(thread)
#else
# error Cannot define thread_local
#endif