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:// ...
随机推荐
- 使用JAVA如何对图片进行格式检查以及安全检查处理
一.通常情况下,验证一个文件是否图片,可以通过以下三种方式: 1).判断文件的扩展名是否是要求的图片扩展名 这种判断是用得比较多的一种方式,不过这种方式非常的不妥,别人稍微的把一个不是图片的文件的扩展 ...
- TDengine陶建辉 自带聚光灯&BGM的半百少年
TDengine,这款定位为“专为物联网而生的大数据平台”,引爆了2019年夏天的软件圈. 2019年7月12日,涛思数据宣布将TDengine的内核(存储和计算引擎)以及社区版100%开源. 201 ...
- (转)Maven创建webapp项目无法修改web版本的问题
maven创建的web app,默认使用的servlet版本是2.3,默认不支持JSTL,为了默认支持JSTL表达式,需要升级servlet到3.0 转:http://blog.sina.com.cn ...
- jQuery Mobile 自定义导航条图标
1.jQuery Mobile 自定义导航条图标
- 用 Flask 来写个轻博客 (6) — (M)VC_models 的关系(one to many)
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 前言 一对多 再一次 sync db How to use ...
- docker x509: certificate has expired or is not yet valid
系统环境:centos 6.5 内核版本:2.6.32-696.1.1.el6.x86_64 程序版本:Docker version 1.7.1, build 786b29d/1.7.1 问题:下载镜 ...
- 使用postman请求响应Invalid CORS request
响应结果 解决方法: 下载之后解压,在Chrome浏览器,打开扩展 chrome://extensions/ 点击“加载已解压的扩展程序”添加我们解压的包,或者直接拖拽.之后我们就可以看到Postma ...
- strcoll - 用当前的区域选项来比较两个字符串
总览 (SYNOPSIS) #include <string.h> int strcoll(const char *s1, const char *s2); 描述 (DESCRIPTION ...
- 添加ali yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #yum clean a ...
- 创建win32 dll 空项目
动态库,多字节 win32 空项目 添加导出头文件 类 导入: #pragma once #ifndef IP_CLASS_DLL_H #define IP_CLASS_DLL_H #pragma ...