您的位置:首页 > 运维架构 > Linux

TLS线程局部存储

2016-03-13 13:21 661 查看

0x01 TLS (Thread Local Storage)

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



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返回的地址各不相同
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息