---
title: GNU不区分__thread和thread_local
description: "```"
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevps/compilation/ug-bisheng/kunpengbisheng_06_0095.html
sourcePath: /source/zh/kunpengdevps/compilation/ug-bisheng/kunpengbisheng_06_0095.html
indexId: a7d3a9fc1e8f3ea5b5d4fed2ddf929203f66e8f81e228c6a9f27eb6b3f2192c874
---
# GNU不区分__thread和thread_local

#### 错误信息

```
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 | 非extern的thread\_local，GNU均处理为\_\_thread |


#### 解决方案

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