C++ 您可以在类或结构中使用线程局部变量吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10999131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you use thread local variables inside a class or structure
提问by pythonic
Like this.
像这样。
struct some_struct
{
// Other fields
.....
__thread int tl;
}
I'm trying to do that but the compiler is giving me this error.
我正在尝试这样做,但编译器给了我这个错误。
./cv.h:16:2: error: '__thread' is only allowed on variable declarations
__thread int tl;
采纳答案by Hristo Iliev
Thread-local storage applies to static variables only. There is no point in making non-static structure or class members thread-local.
线程局部存储仅适用于静态变量。将非静态结构或类成员设为线程本地是没有意义的。
Local (automatic) variables are always specific to the thread that executes the code, but global and static variables are shared among threads since they reside in the data or BSS segment. TLS provides a mechanism to make those global variables local to the thread and that's what the __thread
keyword achieves - it instructs the compiler to create a separate copy of the variable in each thread while lexically it remains a global one (e.g. it can be accessed by different functions called within the same thread of execution).
局部(自动)变量总是特定于执行代码的线程,但全局和静态变量在线程之间共享,因为它们驻留在数据或 BSS 段中。TLS 提供了一种机制来使这些全局变量局部于线程,这就是__thread
关键字所实现的 - 它指示编译器在每个线程中创建变量的单独副本,而在词法上它仍然是一个全局变量(例如,它可以被不同的线程访问)在同一执行线程内调用的函数)。
Non-static class members and structure members are placed in the same place where the object (class or structure) is allocated - either on the stack if an automatic variable is declared or in the heap if new
or malloc()
is used. Either way each thread receives a unique storage location for the variable and __thread
is just not applicable in this case hence the compiler error you get.
非静态类成员和结构成员放置在分配对象(类或结构)的同一位置 - 如果声明了自动变量则放在堆栈中,new
或者如果使用或malloc()
则放在堆中。无论哪种方式,每个线程都会为变量接收一个唯一的存储位置,并且__thread
在这种情况下不适用,因此会出现编译器错误。
回答by NPE
gcc
imposes the following restrictionson the use of __thread
:
gcc
对 的使用施加以下限制__thread
:
The
__thread
specifier may be applied to any global, file-scoped static, function-scoped static, or static data member of a class. It may not be applied to block-scoped automatic or non-static data member.
的
__thread
说明符可以被应用到任何全局,文件范围的静态的,功能范围的静态,或一个类的静态数据成员。它可能不适用于块范围的自动或非静态数据成员。
The __thread
modifier is supported by multiple compiler. It is not inconceivable that the exact restrictions vary somewhat from compiler to compiler.
__thread
多个编译器支持该修饰符。不同编译器的确切限制有所不同,这并非不可想象。
回答by phoxis
C11 standard Section 6.7.1 Paragraph 2
C11 标准第 6.7.1 节第 2 段
At most, one storage-class specifier may be given in the declaration specifiers in a declaration, except that _Thread_local may appear with static or extern.120)
最多可以在声明中的声明说明符中给出一个存储类说明符,除了 _Thread_local 可以与 static 或 extern 一起出现。120)
C11 standard Section 6.7.1 Paragraph 3
C11 标准第 6.7.1 节第 3 段
In the declaration of an object with block scope, if the declaration specifiers include _Thread_local, they shall also include either static or extern. If _Thread_local appears in any declaration of an object, it shall be present in every declaration of that object.
在具有块作用域的对象的声明中,如果声明说明符包含 _Thread_local,则它们还应包含 static 或 extern。如果 _Thread_local 出现在对象的任何声明中,则它应出现在该对象的每个声明中。
回答by Lingxi
You should change __thread int tl;
to thread_local static int tl;
你应该__thread int tl;
改为thread_local static int tl;
回答by C Johnson
According to the old Petzold book 'Programming Windows' (page 1241) you mark a variable as thread local by using the keywords: __declspec (thread). So for instance: __declspec (thread) int iGlobal = 1;
根据旧的 Petzold 书“Programming Windows”(第 1241 页),您可以使用关键字将变量标记为线程本地:__declspec(线程)。例如: __declspec (thread) int iGlobal = 1;
I doubt this could be done in a class though. You can also make the variable static too. [edit] just realized you are probably not running on windows... So I guess for anyone who needs an windows answer, this may be relevant.
我怀疑这可以在课堂上完成。您也可以将变量设为静态。[编辑] 刚刚意识到您可能没有在 Windows 上运行...所以我想对于需要 Windows 答案的任何人来说,这可能是相关的。
回答by Ilya M
Write this way:
这样写:
template <class T> struct S {
thread_local static int tlm;
};
template <> thread_local int S<float>::tlm = 0; // "static" does not appear here
as stated in https://en.cppreference.com/w/cpp/language/storage_duration
如https://en.cppreference.com/w/cpp/language/storage_duration 中所述
回答by Jens Gustedt
For C this makes not much sense, static
(= global) members are only a feature of C++. And so the new C11 standard (that introduces _Thread_local
) doesn't allow it. These beast are allowed basically everywhere were a variable with static storage duration is allowed.
对于 C 这没有多大意义,static
(= global) 成员只是 C++ 的一个特性。所以新的 C11 标准(引入_Thread_local
)不允许这样做。这些野兽基本上在任何地方都是允许的,并且允许具有静态存储持续时间的变量。
For C++ this could make sense inside a class as analogous to a static
member, but if this is allowed by C++11 I have no idea.
对于 C++ 来说,这在类中类似于static
成员是有意义的,但如果 C++11 允许这样做,我不知道。