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. How To Install Nginx on Ubuntu 16.04 zz

    Introduction Nginx is one of the most popular web servers in the world and is responsible for hostin ...

  2. python中使用pyqt做GUI小试牛刀

    import sys from PyQt4 import QtGui , QtCore class LIN(QtGui.QMainWindow): def _init_(self): QtGui.QM ...

  3. 常用python shell

    路径及文件操作 创建目录 os.mkdir(path_str) 列出当前文件夹中文件,存入string list中 os.listdir(path_str) 判断路径是否存在 os.path.exis ...

  4. js实现图片下载

    <img src='src' data-name='自定义名称'><script>//js实现图片下载 function download(){ var name = $('# ...

  5. Ext 目录

    adapter:负责将里面提供第三方底层库(包括Ext自带的底层库)映射为Ext所支持的底层库. build: 压缩后的ext全部源码(里面分类存放). docs: API帮助文档. exmaples ...

  6. Linux用户和用户组的初步知识

    用户组相关:在Linux系统下,当创建一个如叫tom的用户,那么系统同时会创建一个叫tom的用户组linux中用户的信息都是放在/etc/passwd下的,用户的密码经过加密后放在/etc/shado ...

  7. python 自定义过滤器

    文件目录结构: 新建文件并且命名为“templatetags” , 然后复制 __init__.py文件,拷贝到templatetags文件夹里, __pycache__文件夹可以忽略哈,那是程序运行 ...

  8. 字符串匹配&Rabin-Karp算法讲解

    问题描述: Rabin-Karp的预处理时间是O(m),匹配时间O( ( n - m + 1 ) m )既然与朴素算法的匹配时间一样,而且还多了一些预处理时间,那为什么我们还要学习这个算法呢?虽然Ra ...

  9. WSDL格式浅析

    其中,WSDL是一种 XML 格式,用于将网络服务描述为一组端点,这些端点对包含面向文档信息或面向过程信息的消息进行操作.这种格式首先对操作和消息进行抽象描述,然后将其绑定到具体的网络协议和消息格式上 ...

  10. 【置换群】【枚举约数】hdu6038 Function

    把b数组的所有置换群求出来,用数组记录一下每个大小所出现的次数. 然后求a的置换群,对每个置换群求能被其整除的b的置换群的大小总和(只有这些才能满足构造出一个f,且不自相矛盾),然后把它们全都乘起来就 ...