linux线程私有数据

来源:互联网 发布:淘宝企业店铺靠谱吗 编辑:程序博客网 时间:2024/05/01 21:56

今天在看线程的私有数据时,一直想找个例子,实际的验证下,用数据告诉自己:"对,就是那样的,那就是TSD“,于是乎我看到了这个例子

http://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part2/,不得不说IBMdeveloperworkshop这的很好,有很多非常优秀的文章,至于原创与否我没有考证,不下结论,于是乎下面的代码,纷纷的被转载,转载,继续转载。我百度 ”线程私有数据“,前几篇都是来自对IBM的转载,我晕死了,那个例子个人真心感觉不是很好的体现了线程私有数据的特性,另外作者专门说明:以下这个例子没有什么实际意义,只是说明如何使用,以及能够使用这一机制达到存储线程私有数据的目的。

原作者代码:

#include <stdio.h>#include <pthread.h>pthread_key_t   key;void echomsg(int t){        printf("destructor excuted in thread %d,param=%d\n",pthread_self(),t);}void * child1(void *arg){        int tid=pthread_self();        printf("thread %d enter\n",tid);        pthread_setspecific(key,(void *)tid);        sleep(2);        printf("thread %d returns %d\n",tid,pthread_getspecific(key));        sleep(5);}void * child2(void *arg){        int tid=pthread_self();        printf("thread %d enter\n",tid);        pthread_setspecific(key,(void *)tid);        sleep(1);        printf("thread %d returns %d\n",tid,pthread_getspecific(key));        sleep(5);}int main(void){        int tid1,tid2;        printf("hello\n");        pthread_key_create(&key,echomsg);        pthread_create(&tid1,NULL,child1,NULL);        pthread_create(&tid2,NULL,child2,NULL);        sleep(10);        pthread_key_delete(key);        printf("main thread exit\n");        return 0;
好吧,吐槽完毕,上自己的代码,证明下linux的线程私有数据

[fy@localhost pthread_key_create]$ less main.c #include <stdio.h>#include <string.h>#include <pthread.h>#include <unistd.h>#define KEYpthread_key_t key;int tsd=10;void * thread2(void *arg){        printf("before enter thread 2 the global's tsd is %d\n",tsd);        #ifdef KEY                pthread_setspecific(key,(void *)tsd);                key=5;                printf("after thread2 modify the thread2's own tsd is ----- %d\n",pthread_getspecific(key));        #endif        printf("after modify the global's tsd is %d \n",tsd);        pthread_exit((void*)0);        return NULL;}void * thread1(void *arg){        printf("before enter the thread 1 the global's tsd is %d\n",tsd);        pthread_t thid2;        #ifdef KEY                pthread_setspecific(key,(void *)tsd);                key=0;                printf("after thread1 modify ----thread1's own tsd is  %d\n",pthread_getspecific(key));        #endif        pthread_create(&thid2,NULL,thread2,NULL);        pthread_join(thid2,NULL);        #ifdef KEY                printf("after thread2  modify   thread1's own tsd  is ----- %d\n",pthread_getspecific(key));        #endif        printf("after the thread2 the global's tsd is %d\n",tsd);        pthread_exit((void*)0);}void test1(){        pthread_t thid1;        printf("main thread begins running\n");        #ifdef KEY                pthread_key_create(&key,NULL);        #endif        pthread_create(&thid1,NULL,thread1,NULL);        pthread_join(thid1,NULL);        sleep(3);        #ifdef KEY                pthread_key_delete(key);        #endif        printf("main thread exit\n");}int main(int argc, char **argv){        test1();        return 0;}
使宏定义判断为假,可以作对比测试。
运行结果如下:

[fy@localhost pthread_key_create]$ ./run main thread begins runningbefore enter the thread 1 the global's tsd is 10after thread1 modify ----thread1's own tsd is  10before enter thread 2 the global's tsd is 10after thread2 modify the thread2's own tsd is ----- 0after modify the global's tsd is 10 after thread2  modify   thread1's own tsd  is ----- 0after the thread2 the global's tsd is 10main thread exit[fy@localhost pthread_key_create]$ 
线程1把全局变量设置为自己的私有数据,创建线程2,线程2获得数据,数据依旧是10,也就是说线程1修改没有影响全局,线程2设置为自己的私有数据,继续修改之,回到线程1,取出私有数据,还是自己设置的数据,也就是线程2的修改没有影响线程1的私有数据,两个线程都退出,全局变量依旧是10.



原创粉丝点击