如何将变量声明为本地可移植的线程?

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

不幸的是我没有找到任何实际实现_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 

将维基百科的信息与此编译器宏列表相结合,我提出了以下(未经测试的)版本:

 #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