Thread-Local Storage(TLS)

本文介绍了GCC中线程局部存储(TLS)的概念及其实现机制。TLS是一种为每个线程分配独立变量实例的机制,它起源于IA-64处理器特定的ABI,并已扩展到其他处理器。文章详细解释了TLS的运行时模型、地址模型及如何在C/C++中使用__thread关键字声明TLS变量。

http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Thread-Local.html

 

Thread-local storage (TLS ) is a mechanism by which variables are allocated such that there is one instance of the variable per extant thread. The run-time model GCC uses to implement this originates in the IA-64 processor-specific ABI, but has since been migrated to other processors as well. It requires significant support from the linker (ld ), dynamic linker (ld.so ), and system libraries (libc.so and libpthread.so ), so it is not available everywhere.

At the user level, the extension is visible with a new storage class keyword: __thread . For example:

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

The __thread specifier may be used alone, with the extern or static specifiers, but with no other storage class specifier. When used with extern or static , __thread must appear immediately after the other storage class specifier.

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.

When the address-of operator is applied to a thread-local variable, it is evaluated at run-time and returns the address of the current thread's instance of that variable. An address so obtained may be used by any thread. When a thread terminates, any pointers to thread-local variables in that thread become invalid.

No static initialization may refer to the address of a thread-local variable.

In C++, if an initializer is present for a thread-local variable, it must be a constant-expression , as defined in 5.19.2 of the ANSI/ISO C++ standard.

See ELF Handling For Thread-Local Storage for a detailed explanation of the four thread-local storage addressing models, and how the run-time is expected to function.

在Windows下,C语言可以通过Windows API来实现线程本地存储(Thread-Local StorageTLS)。以下是一个简单的示例代码来演示如何在Windows下使用C语言实现线程本地存储。 ```c #include <windows.h> // 声明线程本地存储变量 __declspec(thread) int tls_variable; // 线程函数 DWORD WINAPI ThreadFunction(LPVOID lpParam) { // 设置线程本地存储变量的值 tls_variable = 42; // 在线程内部访问线程本地存储变量的值 printf("Thread local variable value: %d\n", tls_variable); return 0; } int main() { HANDLE hThread; // 创建一个线程 hThread = CreateThread(NULL, 0, ThreadFunction, NULL, 0, NULL); if (hThread == NULL) { printf("Failed to create thread\n"); return 1; } // 等待线程结束 WaitForSingleObject(hThread, INFINITE); // 关闭线程句柄 CloseHandle(hThread); return 0; } ``` 在上面的示例代码中,我们使用`__declspec(thread)`关键字来声明一个线程本地存储变量`tls_variable`。在`ThreadFunction`线程函数中,我们设置了线程本地存储变量的值为42,并在线程内部打印了变量的值。 需要注意的是,使用`__declspec(thread)`关键字声明的线程本地存储变量只能是静态或全局变量,不能是局部变量。 编译运行上述代码后,你会看到输出中显示了线程本地存储变量的值为42。这证明了在线程之间,每个线程都有自己独立的线程本地存储空间。 这只是一个简单的示例,实际应用中,你可以根据需要在线程函数中使用线程本地存储来存储和访问特定于每个线程的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值