pthread_getspecific和pthread_setspecific使用
2015-05-13 09:00
295 查看
pthread_getpecific和pthread_setspecific实现同一个线程中不同函数间共享数据的一种很好的方式。
#more test.c
/*
* =====================================================================================
* Filename: thead.c
* Description: getspecific
* Created: 05/10/2011 12:09:43 AM
* =====================================================================================
*/
#include<stdio.h>
#include<pthread.h>
#include<string.h>
pthread_key_t p_key;
void func1()
{
int *tmp = (int*)pthread_getspecific(p_key);//同一线程内的各个函数间共享数据。
printf("%d is runing in %s\n",*tmp,__func__);
}
void *thread_func(void *args)
{
pthread_setspecific(p_key,args);
int *tmp = (int*)pthread_getspecific(p_key);//获得线程的私有空间
printf("%d is runing in %s\n",*tmp,__func__);
*tmp = (*tmp)*100;//修改私有变量的值
func1();
return (void*)0;
}
int main()
{
pthread_t pa, pb;
int a=1;
int b=2;
pthread_key_create(&p_key,NULL);
pthread_create(&pa, NULL,thread_func,&a);
pthread_create(&pb, NULL,thread_func,&b);
pthread_join(pa, NULL);
pthread_join(pb, NULL);
return 0;
}
#gcc -lpthread test.c -o test
# ./test
2 is runing in thread_func
1 is runing in thread_func
100 is runing in func1
200 is runing in func1
参考资料:http://hi.baidu.com/livetodaywell/blog/item/7d1b7811fbf58af6c2ce79e6.html
#more test.c
/*
* =====================================================================================
* Filename: thead.c
* Description: getspecific
* Created: 05/10/2011 12:09:43 AM
* =====================================================================================
*/
#include<stdio.h>
#include<pthread.h>
#include<string.h>
pthread_key_t p_key;
void func1()
{
int *tmp = (int*)pthread_getspecific(p_key);//同一线程内的各个函数间共享数据。
printf("%d is runing in %s\n",*tmp,__func__);
}
void *thread_func(void *args)
{
pthread_setspecific(p_key,args);
int *tmp = (int*)pthread_getspecific(p_key);//获得线程的私有空间
printf("%d is runing in %s\n",*tmp,__func__);
*tmp = (*tmp)*100;//修改私有变量的值
func1();
return (void*)0;
}
int main()
{
pthread_t pa, pb;
int a=1;
int b=2;
pthread_key_create(&p_key,NULL);
pthread_create(&pa, NULL,thread_func,&a);
pthread_create(&pb, NULL,thread_func,&b);
pthread_join(pa, NULL);
pthread_join(pb, NULL);
return 0;
}
#gcc -lpthread test.c -o test
# ./test
2 is runing in thread_func
1 is runing in thread_func
100 is runing in func1
200 is runing in func1
参考资料:http://hi.baidu.com/livetodaywell/blog/item/7d1b7811fbf58af6c2ce79e6.html
相关文章推荐
- pthread_getspecific和pthread_setspecific使用
- pthread_getspecific和pthread_setspecific使用
- pthread_getspecific()和pthread_setspecific()的使用方法
- pthread_getspecific和pthread_setspecific使用
- pthread_getspecific和pthread_setspecific使用
- 线程局部存储-pthread_getspecific和pthread_setspecific使用
- pthread_getspecific()--读线程私有数据|pthread_setspecific()--写线程私有数据
- pthread_key_setspecific和pthread_key_getspecific 用法
- pthread_setspecific,pthread_getspecific
- pthread_key_setspecific和pthread_key_getspecific 用法
- pthread_once,pthread_key_create,pthread_setspecific,pthread_getspecific
- 线程数据共享:pthread_getspecific和pthread_setspecific
- pthread_getspecific()--读线程私有数据|pthread_setspecific()--写线程私有数据
- pthread_once、pthread_key_create和pthread_getspecific和pthread_setspecific
- pthread_create,pthread_once,pthread_key_create,pthread_key_delete,pthread_getspecific,pthread_setspecific()
- linux下错误使用pthread_mutex_lock导致程序奔溃问题分析
- pthread 学习系列 case2-- 使用互斥锁
- linux中使用pthread_kill函数测试线程是否存活
- Windows下使用pthread
- 使用pthread_create时参数的传递