Sunday, 18 August 2013

How to declare a variable as thread local portably?

How to declare a variable as thread local portably?

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

No comments:

Post a Comment