pthread_getspecific和pthread_setspecific使用
来源:互联网 发布:手机视频软件免费 编辑:程序博客网 时间:2024/04/30 06:58
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
- 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_setspecific,pthread_getspecific
- pthread_once、pthread_key_create和pthread_getspecific和pthread_setspecific
- pthread_once,pthread_key_create,pthread_setspecific,pthread_getspecific
- pthread_setspecific函数与pthread_getspecific函数
- pthread_getspecific()--读线程私有数据|pthread_setspecific()--写线程私有数据
- pthread_getpecific和pthread_setspecific
- pthread_setspecific的一段代码
- pthread_setspecific出现coredump
- pthread_getspecific 线程局部存储
- pthread_getspecific 线程局部存储
- pthread_getspecific()--读线程私有数据
- Easyui tree 从数据库中动态加载数据
- 使用FileZilla搭建FTP服务器
- socket编程补充---一个服务器多个客户端
- Binary Tree Preorder Traversal
- [vb6.0] 快捷键触发事件
- pthread_getspecific和pthread_setspecific使用
- Picasso——针对Android的一个强大的图像下载和缓存库
- 新南威尔士大学预科部(UFS)入学成绩要求
- C++拾遗--多线程:临界区解决子线程的互斥
- IOS开发初识
- 字符编码:ASCII,Unicode和UTF-8
- Apache HBase 1.0.0 发布
- 如何对字符串本身进行操作来实现逆向输出
- js中with、this的用法