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.
- linux线程私有数据
- linux线程私有数据
- Linux线程私有数据
- Linux线程私有数据pthread_key_t
- linux线程私有数据详解
- linux线程的私有数据
- linux线程的私有数据
- linux线程私有数据的保护
- Linux高级程序设计----线程与私有数据
- linux 线程私有数据 (一键多值)
- Linux学习笔记:线程私有数据
- 【Linux系统编程】线程私有数据
- linux线程之线程私有数据 pthread_key_create方法的学习
- 线程的私有数据
- 线程与私有数据
- Posix线程私有数据
- 线程私有数据
- 线程私有数据
- ios6 不能激活,想刷回ios5 ,但是itunes无法显示iphone信息,怎么办
- Python基础05 缩进和选择
- android 获取View的高度
- mysql存储过程详解
- ZENCART晚点出现 -c-.html 网址的 解决办法
- linux线程私有数据
- POJ 1365 因子分解
- 帮你查找Ubuntu最快的源 [复制链接] 电梯直达 复制
- 一个不错的源码资源网站
- dedecms相关知识
- C中的static
- 经典面试题(整理)
- Magic with Merlin: Porter-Duff rules!
- g++命令行详解