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使用的更多相关文章

  1. POSⅨ thread

    POSⅨ thread 简称为pthread,Posix线程是一个POSⅨ标准线程.该标准定义 内部API创建和操纵线程. 编辑本段作用 线程库实行了POSIX线程标准通常称为pthreads.POS ...

  2. pthread_mutex_t

     在Linux中使用线程 http://blog.csdn.net/jiajun2001/article/details/12624923 :LINUX就是这个范围作者   原创作品,允许转载,转载时 ...

  3. 在Linux中使用线程

    我并不假定你会使用Linux的线程,所以在这里就简单的介绍一下.如果你之前有过多线程方面的编程经验,完全可以忽略本文的内容,因为它非常的初级. 首先说明一下,在Linux编写多线程程序需要包含头文件p ...

  4. boost tss.hpp源码分析

    tss.hpp定义了thread_specific_ptr,使用thread local storage 技术 1.在thread目录下的win32和pthread目录下thread_data.hpp ...

  5. pthread_getspecific()--读线程私有数据|pthread_setspecific()--写线程私有数据

    原型: #include <pthread.h> void *pthread_getspecific(pthread_key_t key); int pthread_setspecific ...

  6. 在多线程并发请求Api的场景中,如何控制每个线程的qps

    想了一段时间,给出代码Demo #include <stdio.h> #include <stdlib.h> #include <pthread.h> typede ...

  7. Autorelease返回值的快速释放机制

    + (instancetype)createSark { return [self new];}// callerSark *sark = [Sark createSark]; 编译器改写成了形如下面 ...

  8. PHP 源码学习之线程安全

    从作用域上来说,C语言可以定义4种不同的变量:全局变量,静态全局变量,局部变量,静态局部变量. 下面仅从函数作用域的角度分析一下不同的变量,假设所有变量声明不重名. 全局变量,在函数外声明,例如,in ...

  9. Linux 多线程可重入函数

    Reentrant和Thread-safe 在单线程程序中,整个程序都是顺序执行的,一个函数在同一时刻只能被一个函数调用,但在多线程中,由于并发性,一个函数可能同时被多个函数调用,此时这个函数就成了临 ...

随机推荐

  1. python读写xml文件

    python读取xml文件 xml文件是具有树状结构的,如果想要访问某个叶子结点,必须逐层获取其父结点,要读取某个叶子结点内容用text成员 使用前先加载xml工具包 try: import xml. ...

  2. lr_Analysis Options选项介绍

  3. Chrome浏览器你可以选择知道的知识

    Chrome浏览器我想是每一个前端er必用工具之一吧,一部分原因是它速度快,体积不大,支持的新特性也比其它浏览器多,还有一部分我想就是因为它的控制台功能强大了吧,说它是神器一点也不过分,很方便.但其实 ...

  4. OOX之间的关系

    OOA,OOD,OOP三者关系OOA的分析结果可以作为OOD的需求模型OOD的设计结果作为OOP的指导蓝图OOP负责最终实现目标系统

  5. 转:Google Project Zero挖洞经验整理

    https://www.sec-un.org/google-project-zero%E6%8C%96%E6%B4%9E%E7%BB%8F%E9%AA%8C%E6%95%B4%E7%90%86/ 1. ...

  6. 如何访问mvc 默认的错误页

    在ActionResult 中: public ActionResult Error() { return View("~/Views/Shared/Error.cshtml"); ...

  7. Mindjet Mindmanager复制文件打不开

    概述 使用Mindjet软件画思维导图,保存后得到一个后缀为mmap的文件.复制到一个新的位置,却发现新的文件打不开,导致Mindjet崩溃.这里提供一个解决方案. 解决方案 复制的文件打不开 先打开 ...

  8. BZOJ 2157 旅游(动态树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2157 [题目大意] 支持修改边,链上查询最大值最小值总和,以及链上求相反数 [题解] ...

  9. BZOJ 2342 [Shoi2011]双倍回文(manacher+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2342 [题目大意] 记Wr为W串的倒置,求最长的形如WWrWWr的串的长度. [题解] ...

  10. BZOJ 2430 [Poi2003]Chocolate(贪心+归并排序)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2430 [题目大意] 有一块n*m的矩形巧克力,准备将它切成n*m块. 巧克力上共有n- ...