12

C11 引入了_Thread_local存储类说明符,该说明符可以与staticextern存储类说明符结合使用,以将变量声明为线程本地的。__threadGNU C 编译器套件实现了具有相同语义的存储类说明符。

不幸的是,我没有找到任何真正实现_Thread_local关键字的编译器(我尝试过 gcc、clang 和 SUN studio)。我目前使用以下构造来声明关键字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

我知道这可能不适用于 MSVC 和其他编译器。任何人都可以建议我一种更好的方法来声明thread_local它可以在尽可能多的编译器中工作吗?

编辑

Christoph建议 Microsoft Visual C 允许__declspec(thread). 这是更新的宏定义:

/* 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
4

1 回答 1

13

结合来自维基百科的信息和这个编译器宏列表,我想出了以下(未经测试的)版本:

#ifndef thread_local
# if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
#  define thread_local _Thread_local
# elif defined _WIN32 && ( \
       defined _MSC_VER || \
       defined __ICL || \
       defined __DMC__ || \
       defined __BORLANDC__ )
#  define thread_local __declspec(thread) 
/* note that ICC (linux) and Clang are covered by __GNUC__ */
# elif defined __GNUC__ || \
       defined __SUNPRO_C || \
       defined __xlC__
#  define thread_local __thread
# else
#  error "Cannot define thread_local"
# endif
#endif
于 2013-08-18T12:15:43.677 回答