pthread_getspecific和pthread_setspecific使用
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使用的更多相关文章
- POSⅨ thread
POSⅨ thread 简称为pthread,Posix线程是一个POSⅨ标准线程.该标准定义 内部API创建和操纵线程. 编辑本段作用 线程库实行了POSIX线程标准通常称为pthreads.POS ...
- pthread_mutex_t
在Linux中使用线程 http://blog.csdn.net/jiajun2001/article/details/12624923 :LINUX就是这个范围作者 原创作品,允许转载,转载时 ...
- 在Linux中使用线程
我并不假定你会使用Linux的线程,所以在这里就简单的介绍一下.如果你之前有过多线程方面的编程经验,完全可以忽略本文的内容,因为它非常的初级. 首先说明一下,在Linux编写多线程程序需要包含头文件p ...
- boost tss.hpp源码分析
tss.hpp定义了thread_specific_ptr,使用thread local storage 技术 1.在thread目录下的win32和pthread目录下thread_data.hpp ...
- pthread_getspecific()--读线程私有数据|pthread_setspecific()--写线程私有数据
原型: #include <pthread.h> void *pthread_getspecific(pthread_key_t key); int pthread_setspecific ...
- 在多线程并发请求Api的场景中,如何控制每个线程的qps
想了一段时间,给出代码Demo #include <stdio.h> #include <stdlib.h> #include <pthread.h> typede ...
- Autorelease返回值的快速释放机制
+ (instancetype)createSark { return [self new];}// callerSark *sark = [Sark createSark]; 编译器改写成了形如下面 ...
- PHP 源码学习之线程安全
从作用域上来说,C语言可以定义4种不同的变量:全局变量,静态全局变量,局部变量,静态局部变量. 下面仅从函数作用域的角度分析一下不同的变量,假设所有变量声明不重名. 全局变量,在函数外声明,例如,in ...
- Linux 多线程可重入函数
Reentrant和Thread-safe 在单线程程序中,整个程序都是顺序执行的,一个函数在同一时刻只能被一个函数调用,但在多线程中,由于并发性,一个函数可能同时被多个函数调用,此时这个函数就成了临 ...
随机推荐
- git在window与linux的换行符问题
1:背景.我win7,后端是win10,使用了TortoiseGit工具.我使用ssh,他使用http.仓库是在linux,使用gitLab管理 2:问题.仓库是总监之前建好的.后端把文件add后pu ...
- kube-state-metrics组件的安装调试
在安装全家桶之前,可以先一个一个组件的突破. 上次试了一下node exporter用来导出服务器数据metrics. 而用于导出k8s集群数据的组件就是kube-state-metrics.它寄生于 ...
- electron 编译 sqlite3避坑指南---尾部链接有已经编译成功的sqlite3
electron 编译 sqlite3避坑指南(尾部链接有已经编译成功的sqlite3) sqlite很好用,不需要安装,使用electron开发桌面程序,sqlite自然是存储数据的不二之选,奈何编 ...
- ubuntu16.04编译安装GPAC
参考:http://blog.csdn.net/tianlong_hust/article/details/9273875 1.获取gpac的源代码 sudo apt-get install subv ...
- 5.Spark Streaming流计算框架的运行流程源码分析2
1 spark streaming 程序代码实例 代码如下: object OnlineTheTop3ItemForEachCategory2DB { def main(args: Array[Str ...
- 多线程下,Python Sqlite3报[SQLite objects created in a thread can only be used...]问题
明明加了锁保护,还是出了下面的问题 ProgrammingError: SQLite objects created in a thread can only be used in that same ...
- java 的Calendar类的可视化日历示例
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...
- 590. N叉树的后序遍历
给定一个 N 叉树,返回其节点值的后序遍历. 例如,给定一个 3叉树 : 返回其后序遍历: [5,6,3,2,4,1]. 说明: 递归法很简单,你可以使用迭代法完成此题吗? /* // Definit ...
- 有一种感动叫ACM(WJMZBMR在成都赛区开幕式上的讲话)
各位选手,各位教练,大家好,我是来自清华大学交叉信息学院的陈立杰,今天很荣幸站在这里代表全体参赛选手发言.对于我来说,这是我第一次正式参加ACM的比赛.不过我跟ACM之间的缘分,大概在很早的时候就已经 ...
- 【UOJ #34】多项式乘法
http://uoj.ac/problem/34 看了好长时间的FFT和NTT啊qwq在原根那块磨蹭了好久_(:з」∠)_ 首先设答案多项式的长度拓展到2的幂次后为n,我们只要求出一个g(不是原根)满 ...