linux 线程私有数据之一键多值技术TSD池

linux 线程私有数据之 一键多值技术 进程内的所有线程共享进程的数据空间,因此全局变量为所有线程所共有。但有时线程也需要保存自己的私有数据,这时可以创建线程私有数据(Thread- specific Date)TSD来解决。在线程内部,私有数据可以被各个函数访问,但对

 linux 线程私有数据之 一键多值技术

进程内的所有线程共享进程的数据空间,因此全局变量为所有线程所共有。但有时线程也需要保存自己的私有数据,这时可以创建线程私有数据(Thread- specific Date)TSD来解决。在线程内部,私有数据可以被各个函数访问,但对其他线程是屏蔽的。一个明显的例子是errno,每个线程都有自己的副本,不然由于线程间的切换,在一个线程里输出的很可能是令一线程的出错信息。

线程私有数据采用了一种一键多值的技术,即一个键对应多个数值。访问数据时都是通过键值来访问,好像是对一个变量进行访问,其实是在访问不同的数据。使用 线程私有数据时,首先要为每个线程数据创建一个相关联的键。POSIX中操作线程私有数据的主要通过以下4个函数来实现:pthread_key_create(创建一个键),pthread_setspecific(为一个键设置线程私有数据),pthread_getspecific(从一个键读取线程私有数据),pthread_key_delete(删除一个键)。这几个函数的声明如下:
#include <pthread.h>

int pthread_key_create(pthread_key_t *key, void (*destr_function) (void*));

int pthread_key_delete(pthread_key_t key);

int pthread_setspecific(pthread_key_t key, const void *pointer);

void * pthread_getspecific(pthread_key_t key);

pthread_key_create:从Linux的TSD池中分配一项,将其值赋给key供以后访问使用,它的第一个参数key为指向键值的指针,第二个参数为一个函数指针,如果指针不为空,则在线程退出时将以key所关联的数据为参数调用destr_function(),释放分配的缓冲区。
key一旦被创建,所有线程都可以访问它,但各线程可以根据自己的需要往key中填入不同的值,这就相当于提供了一个同名而不同值的全局变量,一键多值。一键多值靠的是一个关键数据结构数组,即TSD池其结构如下:

static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] ={{0,NULL}};

创建一个TSD就相当于将结构数组中的某一项设置为“in_use”,并将其索引返回给*key,然后设置destructor函数 destr_function。pthread_key_create 创建一个新的线程特定数据Key时,系统搜索其所在进程的 Key 结构数组,找出其中第一个不在使用的元素,并返回该元素的索引键。
pthread_setspecific:该函数将pointer的值(不是内容)与key相关联。用pthread_setspecific为一个键指定新的线程数据时,线程必须先释放原有的线程数据用以回收空间。
pthread_getspecific:通过该函数得到与key相关联的数据。
pthread_key_delete:该函数用来删除一个键,键所占用的内存将被释放。需要注意的是,键占用的内存被释放,与该键关联的线程数据所占用的内存并不被释放。因此,线程数据的释放必须在释放键之前完成。
下面我们通过一个例子来学习使用这几个函数的使用,功能类似errno在线程的使用,我猜想全局变量errno是在创建一个线程pthread_create时利用TSD池为该线程分配一个errno的副本的。
点击(此处)折叠或打开
/*   my_errno.c 
     编译链接:gcc my_errno.c -o my_errno -lpthread 
*/ 
  
// 线程私有数据,一键多值,tsd池 
#include <stdio.h> 
#include <string.h> 
#include <pthread.h> 
  
int my_errno = 0; 
pthread_key_t key; 
  
void print_errno(char *str) 
{ 
    printf("%s my_errno:%d\n",str, my_errno); 
} 
void *thread2(void *arg) 
{ 
    printf("thread2 %ld is running\n",pthread_self()); 
    pthread_setspecific(key, (void *)my_errno); 
    printf("thread2 %ld returns %d\n",pthread_self(),\ 
            pthread_getspecific(key)); 
    my_errno = 2;     
    print_errno("thread2"); 
} 
  
void *thread1(void *arg) 
{ 
    pthread_t thid2; 
  
    printf("thread1 %ld is running\n",pthread_self()); 
    pthread_setspecific(key, (void *)my_errno); 
    pthread_create(&thid2, NULL, thread2, NULL); 
    sleep(2); 
    printf("thread1 %ld returns %d\n",pthread_self(),\ 
         pthread_getspecific(key)); 
    my_errno = 1; 
    print_errno("thread1"); 
} 
  
void destr(void *arg) 
{ 
    printf("destroy memory\n"); 
} 
  
int main(void) 
{ 
    pthread_t thid1; 
  
    printf("main thread begins running. my_errno=%d\n",my_errno); 
    pthread_key_create(&key, destr); 
    pthread_create(&thid1, NULL, thread1, NULL); 
    sleep(4); 
    pthread_key_delete(key); 
     
    printf("main thread exit\n"); 
  
    return 0; 
} 
  ./my_errno 后你会发现虽然my_errno是全局的,但在thread1与thread2保留的是私有数据。


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值