LInux多线程编程----线程特定数据的处理函数
1、pthread_key_t和pthread_key_create()
线程中特有的线程存储, Thread Specific Data 。线程存储有什么用了?他是什么意思了?大家都知道,在多线程程序中,所有线程共享程序中的变量。现在有一全局变量,所有线程都可以使用它,改变它的值。而如果每个线程希望能单独拥有它,那么就需要使用线程存储了。表面上看起来这是一个全局变量,所有线程都可以使用它,而它的值在每一个线程中又是单独存储的。这就是线程存储的意义。
线程存储的具体用法:
(1)创建一个类型为 pthread_key_t 类型的变量。
(2)调用 pthread_key_create() 来创建该变量。该函数有两个参数,第一个参数就是上面声明的 pthread_key_t 变量,第二个参数是一个清理函数,用来在线程释放该线程存储的时候被调用。该函数指针可以设成 NULL ,这样系统将调用默认的清理函数。
(3)当线程中需要存储特殊值的时候,可以调用 pthread_setspcific() 。该函数有两个参数,第一个为前面声明的 pthread_key_t 变量,第二个为 void* 变量,这样你可以存储任何类型的值。
(4)如果需要取出所存储的值,调用 pthread_getspecific() 。该函数的参数为前面提到的 pthread_key_t 变量,该函数返回 void * 类型的值。
2、pthread_once 和 pthread_key
一次性初始化
有时候我们需要对一些posix变量只进行一次初始化,如线程键(我下面会讲到)。如果我们进行多次初始化程序就会出现错误。
在传统的顺序编程中,一次性初始化经常通过使用布尔变量来管理。控制变量被静态初始化为0,而任何依赖于初始化的代码都能测试该变量。如果变量值仍然为0,则它能实行初始化,然后将变量置为1。以后检查的代码将跳过初始化。
但是在多线程程序设计中,事情就变的复杂的多。如果多个线程并发地执行初始化序列代码,可能有2个线程发现控制变量为0,并且都实行初始化,而该过程本该仅仅执行一次。
如果我们需要对一个posix变量静态的初始化,可使用的方法是用一个互斥量对该变量的初始话进行控制。但有时候我们需要对该变量进行动态初始化,pthread_once就会方便的多。
有些事需要一次且仅需要一次执行。通常当初始化应用程序时,可以比较容易地将其放在main函数中。但当你写一个库时,就不能在main里面初始化了,你可以用静态初始化,但使用一次初始化(pthread_once_t)会比较容易些。
3、示例代码及运行结果:
/*
* ThreadSpecificData.c
*
* Created on: Aug 11, 2013
* Author: root
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h> #if 0
char * str_accumulate(char *s){
static char accu[] = {};
strcat(accu, s);
return accu;
}
#endif static pthread_key_t str_key;
static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT;
static void str_alloc_key();
static void str_alloc_destroy_accu(void * accu);
char * str_accumulate(const char *s){
char * accu;
pthread_once(&str_alloc_key_once, str_alloc_key);
accu = (char*)pthread_getspecific(str_key);
if(accu == NULL){
accu = malloc();
if(accu == NULL) return NULL;
accu[] = ;
pthread_setspecific(str_key, (void*)accu);
printf("Thread %lx:allocating buffer at %p\n", pthread_self(), accu);
}
strcat(accu, s);
return accu;
} static void str_alloc_key(){
pthread_key_create(&str_key, str_alloc_destroy_accu);
printf("Thread %lx:allocated key %d\n", pthread_self(), str_key);
} //thread specific data is NULL, this function will be called by this thread.
static void str_alloc_destroy_accu(void *accu){
printf("Thread %lx:freeing buffer at %p\n", pthread_self(), accu);
free(accu);
}
void * process(void * arg){
char * str;
str = str_accumulate("Result of ");
str = str_accumulate((char*)arg);
str = str_accumulate(" thread");
printf("Thread %lx:\"%s\"\n", pthread_self(), str);
return NULL;
} int main(){
char * str;
pthread_t th1,th2;
str = str_accumulate("Main process Result of");
pthread_create(&th1, NULL, process, (char*)"first");
pthread_create(&th2, NULL, process, (char*)"second");
str = str_accumulate("initial thread");
printf("Thread %lx:\"%s\"\n", pthread_self(), str);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
return ;
}
运行结果是:
Thread b757b6c0:allocated key
Thread b757b6c0:allocating buffer at 0x9a99008
Thread b757b6c0:"Main process Result ofinitial thread"
Thread b6d79b40:allocating buffer at 0xb6400468
Thread b6d79b40:"Result of second thread"
Thread b6d79b40:freeing buffer at 0xb6400468
Thread b757ab40:allocating buffer at 0xb6400468
Thread b757ab40:"Result of first thread"
Thread b757ab40:freeing buffer at 0xb6400468
LInux多线程编程----线程特定数据的处理函数的更多相关文章
- Linux系统编程——线程私有数据
在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...
- Linux多线程编程——线程的创建与退出
POSIX线程标准:该标准定义了创建和操纵线程的一整套API.在类Unix操作系统(Unix.Linux.Mac OS X等)中,都使用Pthreads作为操作系统的线程.Windows操作系统也有其 ...
- Linux 多线程编程--线程退出
今天分析项目中进程中虚存一直增长问题,运行10个小时虚存涨到121G ,RSS占用为16G 非常恐怖. Valgrind测试无内存泄漏. 内存32G 64bit系统信息如下: Linux线程使用方式是 ...
- Linux多线程编程——线程的同步
POSIX信号量 posix信号量不同于IPC中的信号量 常用的posix信号量函数 #include <semaphore.h> int sem_init(sem_t* sem,i ...
- LInux多线程编程----线程属性pthread_attr_t
1.每个POSIX线程有一个相连的属性对象来表示属性.线程属性对象的类型是pthread_attr_t,pthread_attr_t 在文件/usr/include/bits/pthreadtypes ...
- 线程特定数据TSD总结
一线程的本质 二线程模型的引入 三线程特定数据 四关键函数说明 五刨根问底啥原理 六私有数据使用演示样例 七參考文档 一.线程的本质 Linux线程又称轻量进程(LWP),也就说线程本质是用进程之间共 ...
- Linux多线程实践(4) --线程特定数据
线程特定数据 int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *)); int pthread_key_ ...
- Linux多线程编程初探
Linux线程介绍 进程与线程 典型的UNIX/Linux进程可以看成只有一个控制线程:一个进程在同一时刻只做一件事情.有了多个控制线程后,在程序设计时可以把进程设计成在同一时刻做不止一件事,每个线程 ...
- ZT 为什么pthread_cond_t要和pthread_mutex_t同时使用 || pthread/Linux多线程编程
为什么线程同步的时候pthread_cond_t要和pthread_mutex_t同时使用 (2009-10-27 11:07:23) 转载▼ 标签: 杂谈 分类: 计算机 举一个例子(http:// ...
随机推荐
- JavaScript 六种继承方式
title: JS的六种继承方式 date: 2017-06-27 05:55:49 tags: JS categories: 学习 --- 继承是面向对象编程中又一非常重要的概念,JavaScrip ...
- SQL Server数据库备份&还原
一.备份 1.登录数据库 2.找到要还原的数据库 右键-任务-备份-添加(路径只写一个,刚开始二个总是报错)-确定 二.还原数据库 这个之间报错了二次 1.报错1:备份集中的数据库与现有数据库“XXX ...
- 使用struts2未登录,不能操作
1.定义拦截器类: 注意登录的action,登录成功在session存入标记(login) import com.opensymphony.xwork2.ActionContext; import c ...
- 深入浅出C语言中的柔性数组
在日常的编程中,有时候需要在结构体中存放一个长度动态的字符串,一般的做法,是在结构体中定义一个指针成员,这个指针成员指向该字符串所在的动态内存空间,例如: typedef struct test { ...
- PHP面试 PHP基础知识 十一(开发环境及相关配置)
开发环境及相关配置 版本控制软件 集中式和分布式 集中式:集中式版本控制工具,版本库集中存放在中央服务器,团队成员里的每个人工作时从中央服务器下载代码,个人修改后再提交到中央服务器 分布式:分布式版本 ...
- 如何在android studio中cordova的混合开发
基于Android Studio 中Cordova的开发 cordova简介 Cordova的前身是PhoneGap 官网: (http://cordova.io) Cordova应是运行在客户端本地 ...
- redis 入门之string
set 用法 #set key value 设置value为字符串的键值对redis> SET key "value" #对不存在的key设置value OK redis& ...
- [轉]Exploit Linux Kernel Slub Overflow
Exploit Linux Kernel Slub Overflow By wzt 一.前言 最近几年关于kernel exploit的研究比较热门,常见的内核提权漏洞大致可以分为几类: 空指针引用, ...
- Codeforces A Mist of Florescence
A Mist of Florescence 题目大意: 事先告诉你每种颜色分别有几个联通块,构造一个不超过 \(50*50\) 的矩形.用 \(A,B,C,D\) 四种颜色来对矩形进行涂色使它满足要求 ...
- bzoj4550 小奇的博弈
我看出了是个 Nimk 问题.... dp我明白意思,我也会推组合数.... 但是...神tm统计答案啊...蒟蒻不会~