我正在尝试使用 __thread 说明符来创建一个线程局部变量。这在以下代码中工作正常:
#include <stdio.h>
#include <pthread.h>
static __thread int val;
int main()
{
val = 10;
}
但是如果我尝试在类中使用 __thread 说明符,如下所示:
#include <stdio.h>
#include <pthread.h>
class A
{
public:
A();
static __thread int val;
};
A::A()
{
val = 10;
}
int main()
{
A a;
}
我收到编译器错误:对“A::val”的 undefined reference
最佳答案
您只声明了静态变量;您还必须在类之外定义它(如果您有多个源文件,则只在一个源文件中):
int __thread A::val;
关于c++ - __thread 说明符在 C++ 类中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12320848/