TLS线程局部存储

0x01 TLS (Thread Local Storage)

为线程单独提供的私有空间
TLS

0x02 gcc中的隐式TLS使用方法

隐式TLS
__thread int number;

显式TLS
pthread_key_create
pthread_getspecific
pthread_setspecific
pthread_key_delete

0x03 windows中的隐式TLS使用方法

隐式TLS
__declspec(thread) int number;

显式TLS
TlsAlloc
TlsGetValue
TlsSetValue
TlsFree

0x04 TLS实现机制

该变量事实上是为每一个线程在堆上申请了一块内存,假设在C++中,有一个全局的位于TLS的对象,则特别须要处理其构造函数

0x05 errno (glibc)

事实上也是一个位于TLS中的变量,其定义为:
#define errno (*__errno_location())
不同的线程调用__errno_location返回的地址各不同样

原文地址:https://www.cnblogs.com/yangykaifa/p/7290004.html