Can you use thread local variables inside a class or structure

五迷三道 提交于 2019-11-30 08:20:53

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).

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.

gcc imposes the following restrictions on the use of __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.

The __thread modifier is supported by multiple compiler. It is not inconceivable that the exact restrictions vary somewhat from compiler to compiler.

C11 standard Section 6.7.1 Paragraph 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)

C11 standard Section 6.7.1 Paragraph 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.

You should change __thread int tl; to thread_local static int tl;

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;

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!