The __thread storage class specifier

Posted rtoax

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The __thread storage class specifier相关的知识,希望对你有一定的参考价值。

https://www.ibm.com/docs/en/xl-c-and-cpp-linux/16.1.0?topic=compatibility-thread-storage-class-specifier

 

The __thread storage class marks a static variable as having thread-local storage duration. This means that, in a multithreaded application, a unique instance of the variable is created for each thread that uses it, and destroyed when the thread terminates. The __thread storage class specifier can provide a convenient way of assuring thread-safety: declaring an object as per-thread allows multiple threads to access the object without the concern of race conditions, while avoiding the need for low-level programming of thread synchronization or significant program restructuring.

The tls_model attribute allows source-level control for the thread-local storage model used for a given variable. The tls_model attribute must specify one of local-exec, initial-exec, local-dynamic, or global-dynamic access method, which overrides the -ftls-model (-qtls) option for that variable. For example:

__thread int i __attribute__((tls_model("local-exec")));

The tls_model attribute allows the linker to check that the correct thread model has been used to build the application or shared library. The linker/loader behavior is as follows:

Table 1. Link time/runtime behavior for thread access models

Access method

Link-time diagnostic

Runtime diagnostic

local-execFails if referenced symbol is imported.Fails if module is not the main program. Fails if referenced symbol is imported (but the linker should have detected the error already).
initial-execNone.dlopen() fails if referenced symbol is not in the module loaded at execution time.
local-dynamicFails if referenced symbol is imported.Fails if referenced symbol is imported (but the linker should have detected the error already).
global-dynamicNone.None.

The specifier can be applied to variables with static storage duration. It cannot be applied to function-scoped or block-scoped automatic variables or non-static data members.

The thread specifier can be either preceded or followed by the static or extern specifier.

__thread int i;      
extern __thread struct state s;      
static __thread char *p;

 

C++ onlyA thread variable must be initialized with a constant expression.C++ only

Applying address operator (&) to a thread-local variable returns the runtime address of the current thread's instance of the variable. That thread can pass this address to any other thread; however, when the first thread terminates, any pointers to its thread-local variables become invalid.

Parent topic:

Extensions for GNU C compatibility

Related reference:

-ftls-model (-qtls)

以上是关于The __thread storage class specifier的主要内容,如果未能解决你的问题,请参考以下文章

test for thread local storage

java_线程创建的两种方法

Thread-local storage

Collect logs from the servers

requests源码分析

9_Permanent Storage