linux学习之十五---多线程私有数据
来源:互联网 发布:淘宝的管控记录是什么 编辑:程序博客网 时间:2024/06/07 05:10
linux多线程 私有数据
在多线程环境下,进程内的所有线程共享进程的数据空间,因此全局变量为所有线程共有。在程序设计中有时需要保存线程自己的全局变量,这种特殊的变量仅在某个线程内部有效。线程私有数据采用立一种被称为一键多值的技术,即一个键对应多个数值。访问数据都是通过键值来访问,好像是对一个变量进行访问,其实是在访问不同的数据。使用线程私有数据时,首先要为每个线程数据创建一个相关联的键。在各个线程内部,都使用这个公用的键来指代线程数据,但是在不同的线程中,这个键代表的数据是不同的。
操作线程私有数据的函数主要有4个:
pthread_key_create(创建一个键);pthread_setspecific(为一个键设置线程私有数据);
pthread_getspecific(从一个键读取线程私有数据);
pthread_key_delete(删除一个键)。
函数参数:
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
调用 pthread_key_create() 来创建该变量,第一个参数就是上面声明的 pthread_key_t 变量,第二个参数是一个清理函数,用来在线程释放该线程存储的时候被调用。该函数指针可以设成 NULL ,这样系统将调用默认的清理函数。int pthread_setspecific(pthread_key_t key, const void *value)
当线程中需要存储特殊值的时候,可以调用 pthread_setspcific() 。该函数有两个参数,第一个为前面声明的 pthread_key_t 变量,第二个为 void* 变量,这样你可以存储任何类型的值。void *pthread_getspecific(pthread_key_t key)
如果需要取出所存储的值,调用 pthread_getspecific() 。该函数的参数为前面提到的 pthread_key_t 变量,该函数返回 void * 类型的值。
int pthread_key_delete(pthread_key_t key)
注销一个 TSD 使用 pthread_key_delete() 函数。该函数并不检查当前是否有线程正在使用该 TSD,也不会调用清理函数(destructor function),而只是将 TSD 释放以供下一次调用 pthread_key_create() 使用。
用法示例:
#include<stdio.h>#include<string.h>#include<pthread.h>pthread_key_t key;void* thread2(void*arg){ int tsd=2; printf("thread2 %d is running\n",pthread_self()); pthread_setspecific(key,(void*)tsd); sleep(1); printf("thread22 %d returns %d addr:%p\n",pthread_self(),pthread_getspecific(key),&tsd);}void* thread1(void*arg){ int tsd=5; pthread_t thid2; printf("thread1 %d is running\n",pthread_self()); pthread_setspecific(key,(void*)tsd); pthread_create(&thid2,NULL,thread2,NULL); sleep(2); printf("thread11 %d returns %d addr:%p\n",pthread_self(),pthread_getspecific(key),&tsd);}int main(){ pthread_t thid1; printf("main thread begins running\n"); pthread_key_create(&key,NULL); pthread_create(&thid1,NULL,thread1,NULL); sleep(3); pthread_key_delete(key); printf("main thread exit\n"); return 0;}
运行结果:
这个程序只是单纯调用了设置私有数据的变量,但是这个没有说明全局变量如何在每个线程中保持独立。
在多线程中使用私有数据:
#include<stdio.h>#include<string.h>#include<pthread.h>pthread_key_t key;int tsd=0;void* thread2(void*arg){ // tsd=2; printf("thread2 %d is running\n",pthread_self()); sleep(1); printf("thread22 %d returns %d\n",pthread_self(),tsd);}void* thread1(void*arg){ tsd=5; // tsd+=100; pthread_t thid2; printf("thread1 %d is running\n",pthread_self()); pthread_setspecific(key,(void*)tsd); tsd+=100;//仅在该线程内加100 pthread_create(&thid2,NULL,thread2,NULL); sleep(2); printf("thread11 %d returns %d\n",pthread_self(),pthread_getspecific(key));}int main(){ pthread_t thid1; printf("main thread begins running\n"); pthread_key_create(&key,NULL); pthread_create(&thid1,NULL,thread1,NULL); sleep(3); pthread_key_delete(key); printf("main thread exit\n"); return 0;}
运行结果:
分析:
运行可见,在tsd与thread1中的key绑定后,执行tsd+=100操作后,只对thread1有效,即返回105,但是对thread2函数来说,依然是5。
0 0
- linux学习之十五---多线程私有数据
- Linux多线程基础学习(八)私有数据
- linux线程之线程私有数据 pthread_key_create方法的学习
- 多线程编程之九:线程私有数据
- linux多线程学习笔记六--一次性初始化和线程私有数据
- Linux学习笔记:线程私有数据
- 【C/C++多线程编程之十】pthread线程私有数据
- Linux学习之十五(sed命令)
- linux设备驱动开发学习之旅--使用文件私有数据的字符设备驱动
- Linux私有数据编程
- linux线程私有数据
- linux线程私有数据
- Linux线程私有数据
- linux驱动开发之字符设备--私有数据和container_of
- Linux学习 十五单元
- MySQL学习笔记之十五 更新日志文件恢复数据
- Hasen的linux设备驱动开发学习之旅--使用文件私有数据的字符设备驱动
- linux学习之二十五---ar的详细解释
- ts 1107
- MySql学习之路2之入门SQL操作
- Makefile
- C#数据结构-树 data Structure Tree
- 设计模式(一)——Factory模式
- linux学习之十五---多线程私有数据
- UITableViewCell 多余的分割线不显示
- 20141107 【 单片机——精准时钟——中断 】 精准计时秒表
- learning OpenCV(4)
- 解决MyEclipse不能自动提示代码的功能
- JSP隐式对象到servlet API的映射
- 1598 find the most comfortable road
- zigbee中profile,cluster的认识
- How to install Hadoop 2.2.0