深入Pthread(四):一次初始化-pthread_once_t
来源:互联网 发布:淘宝上卖什么东西好卖 编辑:程序博客网 时间:2024/04/29 12:10
深入Pthread(四):一次初始化-pthread_once_t
用到的API:
pthread_once_t once_control = PTHREAD_ONCE_INIT;
int pthread_once(pthread_once_t* once_control, void (*init_routine)(void));
有些事需要一次且仅需要一次执行。通常当初始化应用程序时,可以比较容易地将其放在main函数中。但当你写一个库时,就不能在main里面初始化了,你可以用静态初始化,但使用一次初始化(pthread_once_t)会比较容易些。
例程:
#include <pthread.h>
#include "errors.h"
pthread_once_t once_block = PTHREAD_ONCE_INIT;
pthread_mutex_t mutex;
/*This is the one-time initialization routine. It will be
* called exactly once, no matter how many calls to pthread_once
* with the same control structure are made during the course of
* the program.
*/
void once init routine (void)
{
int status;
status = pthread_mutex_init (&mutex, NULL);
if (status != 0)
err_abort (status, "Init Mutex");
}
/* Thread start routine that calls pthread_once.
*/
*/
void *thread routine (void *arg)
{
{
int status;
status = pthread_once (&once_block, once_init_routine);
if (status != 0)
err_abort (status, "Once init");
status = pthread_mutex_lock (&mutex);
if (status != 0)
err_abort (status, "Lock mutex");
printf ("thread routine has locked the mutex./n");
status = pthread_once (&once_block, once_init_routine);
if (status != 0)
err_abort (status, "Once init");
status = pthread_mutex_lock (&mutex);
if (status != 0)
err_abort (status, "Lock mutex");
printf ("thread routine has locked the mutex./n");
status = pthread_mutex_unlock (&mutex);
if (status ! = 0)
err_abort (status, "Unlock mutex");
return NULL;
if (status ! = 0)
err_abort (status, "Unlock mutex");
return NULL;
}
int main (int argc, char *argv[])
{
{
pthread_t thread_id;
char *input, buffer[64];
int status;
status = pthread_create (&thread_id, NULL, thread_routine, NULL);
if (status != 0)
err_abort (status, "Create thread");
status = pthread_once (&once_block, once_init_routine);
if (status != 0)
err_abort (status, "Once init");
status = pthread_mutex_lock (&mutex);
if (status != 0)
err_abort (status, "Lock mutex");
printf ("Main has locked the mutex./n");
status = pthread_mutex_unlock (&mutex);
if (status != 0)
err_abort (status, "Unlock mutex");
status = pthread_join (thread_id, NULL);
if (status != 0)
err_abort (status, "Join thread");
return 0;
char *input, buffer[64];
int status;
status = pthread_create (&thread_id, NULL, thread_routine, NULL);
if (status != 0)
err_abort (status, "Create thread");
status = pthread_once (&once_block, once_init_routine);
if (status != 0)
err_abort (status, "Once init");
status = pthread_mutex_lock (&mutex);
if (status != 0)
err_abort (status, "Lock mutex");
printf ("Main has locked the mutex./n");
status = pthread_mutex_unlock (&mutex);
if (status != 0)
err_abort (status, "Unlock mutex");
status = pthread_join (thread_id, NULL);
if (status != 0)
err_abort (status, "Join thread");
return 0;
}
author: david(heaven.hell.or@gmail.com)
page:http://code.google.com/p/heavenhell/
- 深入Pthread(四):一次初始化-pthread_once_t
- 深入Pthread(四):一次初始化-pthread_once_t
- 深入Pthread(四):一次初始化-pthread_once_t
- 深入Pthread(四):一次初始化-pthread_once_t
- 一次初始化-pthread_once_t
- 一次初始化-pthread_once_t pthread_once 函数
- 【pthread系列-8】POSIX线程中的“一次初始化”
- Linux Pthread 深入解析
- Linux Pthread 深入解析
- Linux Pthread 深入解析
- Linux Pthread 深入解析
- Linux Pthread 深入解析
- Linux Pthread 深入解析
- 深入Pthread(五):线程属性
- 深入Pthread(五):线程属性
- 深入Pthread(五):线程属性
- 深入Pthread(五):线程属性
- linux: Linux Pthread 深入解析
- 魔盒(MagicBox)1.0 beta3 发布下载,更快更稳定
- Windows应用程序开发入门到精通九:安全的配置并部署.NET Windows Forms应用程序
- hbm2dll配置自动生成数据库的几点小结
- 韩国SK在华投资5亿进军B2C SNS已非战略重心
- VC6编辑时提示This file has been modified outside of the source of editor.Do you want to reload it?解决方法
- 深入Pthread(四):一次初始化-pthread_once_t
- 地面国标:地狱还是天堂?
- KEIL里的irq关键字
- 电冰箱原理
- MSDE安装过程停止不动
- MyEclipse 插件安装
- 冰箱不能制冷,过滤器冰堵问题
- MSDE安装自动回退
- IBM Rational Robot 安装过程提示NuTCRACKERService出错