pthread_once 和 pthread_key
http://blog.csdn.net/rickyguo/article/details/6259410
一次性初始化
有时候我们需要对一些posix变量只进行一次初始化,如线程键(我下面会讲到)。如果我们进行多次初始化程序就会出现错误。
在传统的顺序编程中,一次性初始化经常通过使用布尔变量来管理。控制变量被静态初始化为0,而任何依赖于初始化的代码都能测试该变量。如果变量值仍然为0,则它能实行初始化,然后将变量置为1。以后检查的代码将跳过初始化。
但是在多线程程序设计中,事情就变的复杂的多。如果多个线程并发地执行初始化序列代码,可能有2个线程发现控制变量为0,并且都实行初始化,而该过程本该仅仅执行一次。
如果我们需要对一个posix变量静态的初始化,可使用的方法是用一个互斥量对该变量的初始话进行控制。但有时候我们需要对该变量进行动态初始化,pthread_once就会方便的多。
函数原形:
pthread_once_t once_control=PTHREAD_ONCE_INIT;
int pthread_once(pthread_once_t *once_control,void(*init_routine)(void));
参数:
once_control 控制变量
init_routine 初始化函数
返回值:
若成功返回0,若失败返回错误编号。
类型为pthread_once_t的变量是一个控制变量。控制变量必须使用PTHREAD_ONCE_INIT宏静态地初始化。
pthread_once函数首先检查控制变量,判断是否已经完成初始化,如果完成就简单地返回;否则,pthread_once调用初始化函数,并且记录下初始化被完成。如果在一个线程初始时,另外的线程调用pthread_once,则调用线程等待,直到那个现成完成初始话返回。
下面就是该函数的程序例子:
#include <pthread.h>
pthread_once_t once=PTHREAD_ONCE_INIT;
pthread_mutex_t mutex;
void once_init_routine(void)
{
int status;
status=pthread_mutex_init(&mutex,NULL);
if(status==0)
printf(“Init success!,My id is %u”,pthread_self());
}
void *child_thread(void *arg)
{
printf(“I’m child ,My id is %u”,pthread_self());
pthread_once(&once,once_init_routine);
}
int main(int argc,char *argv[ ])
{
pthread_t child_thread_id;
pthread_create(&child_thread_id,NULL,child_thread,NULL);
printf(“I’m father,my id is %u”,pthread_self());
pthread_once(&once_block,once_init_routine);
pthread_join(child_thread_id,NULL);
}
线程的私有数据
在进程内的所有线程共享相同的地址空间,任何声明为静态或外部的变量,或在进程堆声明的变量,都可以被进程所有的线程读写。那怎样才能使线程序拥有自己的私有数据呢。
posix提供了一种方法,创建线程键。
函数原形:
int pthread_key_create(pthread_key *key,void(*destructor)(void *));
参数:
key 私有数据键
destructor 清理函数
返回值:
若成功返回0,若失败返回错误编号
第一个参数为指向一个键值的指针,第二个参数指明了一个destructor函数(清理函数),如果这个参数不为空,那么当每个线程结束时,系统将调用这个函数来释放绑定在这个键上的内存块。这个函数常和函数pthread_once一起使用,为了让这个键只被创建一次。函数pthread_once声明一个初始化函数,第一次调用pthread_once时它执行这个函数,以后的调用将被它忽略。
下面是程序例子:
#include <pthread.h>
pthread_key_t tsd_key;
pthread_once_t key_once=PTHREAD_ONCE_INIT;
void once_routine(void)
{
int status;
status=pthread_key_create(&tsd_key,NULL);
if(status=0)
printf(“Key create success! My id is %u/n”,pthread_self());
}
void *child_thread(void *arg)
{
printf(“I’m child,My id is %u/n”,pthread_self());
pthread_once(&key_once,once_routine);
}
int main(int argc,char *argv[ ])
{
pthread_t child_thread_id;
pthread_create(&child_thread_id,NULL,child_thread,NULL);
printf(“I’m father,my id is%u/n”,pthread_self());
pthread_once(&key_once,once_routine);
}
pthread_once 和 pthread_key的更多相关文章
- LInux多线程编程----线程特定数据的处理函数
1.pthread_key_t和pthread_key_create() 线程中特有的线程存储, Thread Specific Data .线程存储有什么用了?他是什么意思了?大家都知道,在多线程程 ...
- 线程私有数据和pthread_once
#include <stdio.h> #include <pthread.h> pthread_key_t key; pthread_once_t ponce = PTHREA ...
- pthread_once()使用(某个时间在整个程序中仅执行一次,不确定是那个线程)
在多线程环境中,有些事仅需要执行一次.通常当初始化应用程序时,可以比较容易地将其放在main函数中.但当你写一个库时,就不能在main里面初始化了,你可以用静态初始化,但使用一次初始化(pthread ...
- pthread_once()函数详解
转自:pthread_once()函数详解 pthread_once()函数详解 在多线程环境中,有些事仅需要执行一次.通常当初始化应用程序时,可以比较容易地将其放在main函数中.但当你写一个库 ...
- pthread_once详解和使用
转自:pthread_once()函数详解 .pthread_once()使用 在多线程环境中,有些事仅需要执行一次.通常当初始化应用程序时,可以比较容易地将其放在main函数中.但当你写一个库时,就 ...
- pthread_once重塑singleton模式
单件模式是非线程安全的: // Single threaded version class Foo { private Helper helper = null; public Helper getH ...
- 线程的创建,pthread_create,pthread_self,pthread_once
typedef unsigned long int pthread_t; //come from /usr/include/bits/pthreadtypes.h int pthread_create ...
- pthread_once函数的简单示例
/*一次性初始化 int pthread_once(pthread_once_t *once_control, void (*init_routine) (void)) 本函数使用初值为PTHREAD ...
- pthread线程初始化(pthread_once)
pthread_once 语法 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); #include ...
随机推荐
- localhost 127.0.0.1
No1: localhost也叫local ,正确的解释是:本地服务器 127.0.0.1在windows等系统的正确解释是:本机地址(本机服务器) 他们的解析通过本机的host文件,windows自 ...
- 1.Redis的应用场景
转自:http://www.runoob.com/redis/redis-tutorial.html Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在 ...
- ajax方式表单拦截
html <!DOCTYPE html> <html> <head> <title></title> <meta charset=&q ...
- Spring boot 执行jar文件 方式
项目jar包名wxo.jar 清理,打包,跳过测试(不测试) mvn clean package -Dmaven.test.skip=true 后台执行(默认环境) nohup java -jar w ...
- tensorflow ValueError: Cannot feed value of shape (5000,) for Tensor 'output:0', which has shape '(?, 10)'
提供的训练数据和定义的模型之间的维度不对应. 在MNIST手写数字识别时,在 mnist = input_data.read_data_sets("MNIST_data/") 中, ...
- C++17尝鲜:类模板中的模板参数自动推导
模板参数自动推导 在C++17之前,类模板构造器的模板参数是不能像函数模板的模板参数那样被自动推导的,比如我们无法写 std::pair a{1, "a"s}; // C++17 ...
- 有3D效果的进度条
// The Unofficial Newsletter of Delphi Users - Issue #12 - February 23rd, 1996 unit Percnt3d; (* TPe ...
- 调整Mic音量
uses MMSystem; function GetLineInHandle(AudioType : integer) : integer;var i : integer; AudioCaps ...
- es进行curl请求时报错:missing authentication token for REST request
把host 修改一下即可 ELASTICSEARCH_HOST=http://user:password@URL:port
- 122. Best Time to Buy and Sell Stock II (Array;Greedy)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...