鲲鹏社区首页
中文
注册
开发者
我要评分
文档获取效率
文档正确性
内容完整性
文档易理解
在线提单
论坛求助

GNU不区分__thread和thread_local

错误信息

1
2
3
4
5
6
error: thread-local declaration of 'test' with static initialization follows declaration with dynamic initialization
    3 |  __thread long test;
      |                ^
note: previous declaration is here
    1 |  extern thread_local long test;
      |                           ^

问题介绍

__thread标识符和thread_local标识符实际存在区别,__thread是static initialization,thread_local是dynamic initialization,不能同时指定。GNU在实现上采用如下处理逻辑:

源码

处理逻辑

extern __thread a; extern thread_local a;

以靠后的标识符为准

__thread a; extern thread_local;

无论顺序如何,均为__thread

extern __thread a;thread_local a;

无论顺序如何,均为__thread

非extern的thread_local,GNU均处理为__thread

解决方案

  • 按实际代码需求修改声明;
  • 使用clang-tidy工具可以识别该类问题并提供修改建议。