线程私有数据
2014-07-27 21:16
477 查看
#include <stdio.h> #include <string.h> #include <pthread.h> #include <unistd.h> pthread_key_t key; void * thread2(void *arg) { int tsd = 5; printf("thread2 %lu is running\n",pthread_self()); pthread_setspecific(key,(void *)tsd);/* 将tsd值为5设置到key中*/ printf("thread2 %lu returns %d\n",pthread_self(),pthread_getspecific(key)); return NULL; } void * thread1(void *arg) { int tsd = 0; pthread_t thid2; printf("thread1 %lu is running \n",pthread_self()); pthread_setspecific(key,(void *)tsd);/*将tsd值为 0设置到key中*/ // printf("thread1 %lu returns %d\n",pthread_self(),pthread_getspecific(key)); pthread_create(&thid2,NULL,thread2,NULL); sleep(2); printf("thread1 %lu returns %d\n",pthread_self(),pthread_getspecific(key)); return NULL; } void test1() { pthread_t thid1; printf("main thread begins running\n"); pthread_key_create(&key,NULL); pthread_create(&thid1,NULL,thread1,NULL); sleep(3); pthread_key_delete(key); printf("main thread exit\n"); } int main(int argc, char **argv) { #if 1 test1(); #endif return 0; }
相关文章推荐
- 线程控制-私有数据 转自monalisa's_blog
- 12.6 线程控制_线程私有数据
- Posix线程编程指南(2)——线程私有数据
- linux线程之线程私有数据 pthread_key_create方法的学习
- 线程私有数据
- Posix线程编程指南(2)--线程私有数据
- 线程私有数据的介绍与使用(TSD)
- 线程私有数据thread-specific
- linux下C语言多线程(二)线程的私有数据
- 线程控制--私有数据
- Posix线程编程指南(2)--线程私有数据
- 线程控制--私有数据
- pthread线程私有数据
- unix/linux下线程私有数据实现原理及使用方法
- pthread---一次性初始化&&线程私有数据
- linux api笔记(6):线程(四) 线程私有数据
- 线程私有数据(Thread-Specific Data)
- 线程私有数据
- linux下C语言多线程(二)线程的私有数据
- 线程私有数据TSD