线程特定数据

1. 在单线程程序中,我们经常用到全局变量以实现在多个函数间共享数据。
2. 在多线程环境下,由于数据空间是共享的,因此全局变量也为所有线程共有。
3. 但有时应用程序设计中有必要提供私有的全局变量,仅在某个线程中有效,却可以跨多个函数访问。

4. POSIX线程库通过维护一定的数据结构来解决这个问题,这些个数据成为TSD(Thread-specific Data)。



相关函数:

int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
int pthread_key_delete(pthread_key_t key);
void *pthread_getspecific(pthread_key_t key);
int pthread_setsepcific(pthread_key_t key, const void *value);
int pthread_once(pthread_once_t *once_control, void(*init_routine)(void));
pthread_once_t once_control=PTHREAD_ONCE_INIT;

#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#define ERR_EXIT(m) do{perror(m);exit(EXIT_FAILURE);}while(0)
pthread_key_t key_tsd;
pthread_once_t once_control = PTHREAD_ONCE_INIT;
struct tsd
{
    pthread_t tid;
    char *str;
};
void destroy_routine(void *value)
{
    printf("destroy\n");  
    free(value);
}
void once_routine(void)
{
    pthread_key_create(&key_tsd,destroy_routine);   
    printf("key init....\n");
}
void* thread_routine(void *arg)
{
   // pthread_once(&once_control,once_routine);    
    tsd *value=(tsd*)malloc(sizeof(tsd));
    value->tid=pthread_self();
    value->str=(char*)arg;
    pthread_setspecific(key_tsd, value);
    printf("%s setspecific %p\n",(char*)arg,value);
    value=(tsd*)pthread_getspecific(key_tsd);
    printf("tid=%x,str=%s\n",(int)value->tid,value->str);
    sleep(2);
    value=(tsd*)pthread_getspecific(key_tsd);   
    printf("tid=%x,str=%s\n",(int)value->tid,value->str);
    return NULL;
}
int main()
{
    pthread_key_create(&key_tsd,destroy_routine);
    pthread_t tid1,tid2;
    char ch1[]="thread1";
    char ch2[]="thread2";
    pthread_create(&tid1,NULL,thread_routine,(void*)ch1);
    pthread_create(&tid2,NULL,thread_routine,(void*)ch2);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    pthread_key_delete(key_tsd);
    return 0;
}
                                      

程序运行结果:可以发现程序分别输出了自己的线程名称和自己value的地址,说明value=(tsd*)pthread_getspecific(key_tsd)输入了不同的key_tsd,互不影响,各自有自己的“全局变量”。

pthread_once(&once_control,once_routine);这个程序只会在进入的第一个线程里执行once_routine函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值