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的更多相关文章

  1. LInux多线程编程----线程特定数据的处理函数

    1.pthread_key_t和pthread_key_create() 线程中特有的线程存储, Thread Specific Data .线程存储有什么用了?他是什么意思了?大家都知道,在多线程程 ...

  2. 线程私有数据和pthread_once

    #include <stdio.h> #include <pthread.h> pthread_key_t key; pthread_once_t ponce = PTHREA ...

  3. pthread_once()使用(某个时间在整个程序中仅执行一次,不确定是那个线程)

    在多线程环境中,有些事仅需要执行一次.通常当初始化应用程序时,可以比较容易地将其放在main函数中.但当你写一个库时,就不能在main里面初始化了,你可以用静态初始化,但使用一次初始化(pthread ...

  4. pthread_once()函数详解

    转自:pthread_once()函数详解 pthread_once()函数详解   在多线程环境中,有些事仅需要执行一次.通常当初始化应用程序时,可以比较容易地将其放在main函数中.但当你写一个库 ...

  5. pthread_once详解和使用

    转自:pthread_once()函数详解 .pthread_once()使用 在多线程环境中,有些事仅需要执行一次.通常当初始化应用程序时,可以比较容易地将其放在main函数中.但当你写一个库时,就 ...

  6. pthread_once重塑singleton模式

    单件模式是非线程安全的: // Single threaded version class Foo { private Helper helper = null; public Helper getH ...

  7. 线程的创建,pthread_create,pthread_self,pthread_once

    typedef unsigned long int pthread_t; //come from /usr/include/bits/pthreadtypes.h int pthread_create ...

  8. pthread_once函数的简单示例

    /*一次性初始化 int pthread_once(pthread_once_t *once_control, void (*init_routine) (void)) 本函数使用初值为PTHREAD ...

  9. pthread线程初始化(pthread_once)

    pthread_once 语法 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); #include ...

随机推荐

  1. windows与linux环境查看jdk安装路径

    windows: set java_home 查看jdk安装路径 java -version 查看jdk版本 linux: whereis java which java(java执行路径) echo ...

  2. const对象,指向const对象的指针 和 const 指针

    const对象: const对象声明时必须赋初值,该值在编译阶段确定,不可在程序中修改. const修饰符既可放在类型名前也可放在类型名后,通常放在类型名前.不过放在类型名后易于理解. const i ...

  3. ubuntu 16.04 install wine

    from: https://wiki.winehq.org/Ubuntu If your system is 64 bit, enable 32 bit architecture (if you ha ...

  4. 【转】Phong和Blinn-Phong光照模型

    来自:http://www.cnblogs.com/bluebean/p/5299358.html Phong和Blinn-Phong是计算镜面反射光的两种光照模型,两者仅仅有很小的不同之处. 1.P ...

  5. spring浏览器国际化的原理

    We will add two languages support to our application: English and German. Depending on the locale se ...

  6. .net数据库连接防注入参数查询 命令执行 读取 备份 导出导入转化XML格式

    ADO.NET是一组类库,让我们通过程序的方式访问数据库.SYSTEM.DATA这个类提供了统一的接口访问Oracle MSSQL Access.像SYSTEM.IO类操作文件一样. **connec ...

  7. vim使用方法:

    vim使用方法: 模式: 编辑模式.未编辑模式.命令行模式 i 插入形式进入编辑模式 a 增加 o 下行编辑 O 上行插入 : 进入命令行模式 esc 退出编辑模式 wq 保存文件 yy 复制 p 粘 ...

  8. WebDriverException:Element is not clickable at point - selenium执行过程中遇到的相关报错

    Element is not clickable at point (x, y) 这段可以忽略:本文来自 https://www.cnblogs.com/lozz/p/9947430.html 引起这 ...

  9. win7安装SQL2005出现29506错误

    解决方法: 假设下载的文件名为SQLServer2005_SSMSEE.msi ,并且放在F盘根目录下, 新建一个文本文件,输入msiexec /i F:\SQLServer2005_SSMSEE.m ...

  10. Redis阅读目录

    一.Redis简介 点击链接查看:https://www.cnblogs.com/hwlong/p/9325986.html 二.Redis安装及基本配置 点击链接查看:https://www.cnb ...