有些事需要且只能执行一次(比如互斥量初始化)。通常当初始化应用程序时,可以比较容易地将其放在main函数中。但当你写一个库函数时,
就不能在main里面初始化了,你可以用静态初始化,但使用一次初始(pthread_once_t)会比较容易些。
二、如何进行一次性初始化
1、首先要定义一个pthread_once_t变量,这个变量要用宏PTHREAD_ONCE_INIT初始化。然后创建一个与控制变量相关的初始化函数
pthread_once_t once_control = PTHREAD_ONCE_INIT;
void init_routine()
{
//初始化互斥量
//初始化读写锁
......
}
2、接下来就可以在任何时刻调用pthread_once函数
int pthread_once(pthread_once_t* once_control, void (*init_routine)(void));
功能:本函数使用初值为PTHREAD_ONCE_INIT的once_control变量保证init_routine()函数在本进程执行序列中仅执行一次。在多线程编程
环境下,尽管pthread_once()调用会出现在多个线程中,init_routine()函数仅执行一次,究竟在哪个线程中执行是不定的,是由内核调度来决定。
3、Linux Threads使用互斥锁和条件变量保证由pthread_once()指定的函数执行且仅执行一次。实际"一次性函数"的执行状态有三种:
NEVER(0)、IN_PROGRESS(1)、DONE (2),用once_control来表示pthread_once()的执行状态:
1)、如果once_control初值为0,那么 pthread_once从未执行过,init_routine()函数会执行。
2)、如果once_control初值设为1,则由于所有pthread_once()都必须等待其中一个激发"已执行一次"信号, 因此所有pthread_once ()都会陷入永久
的等待中,init_routine()就无法执行
3)、如果once_control设为2,则表示pthread_once()函数已执行过一次,从而所有pthread_once()都会立即 返回,init_routine()就没有机会执行
当pthread_once函数成功返回,once_control就会被设置为2
三、手册
PTHREAD_ONCE(3P) POSIX Programmer’s Manual PTHREAD_ONCE(3P)
PROLOG
This manual page is part of the POSIX Programmer’s Manual. The Linux implementation of this interface may differ (con-
sult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on
Linux.
//这只是POSIX的手册,Linux对这个接口的实现可能不一样,或者有的根本没有实现这个接口
NAME
pthread_once - dynamic package initialization
//动态初始化
SYNOPSIS
#include
//头文件
int pthread_once(pthread_once_t *once_control,
void (*init_routine)(void));
pthread_once_t once_control = PTHREAD_ONCE_INIT;
DESCRIPTION
The first call to pthread_once() by any thread in a process, with a given once_control, shall call the init_routine with
no arguments. Subsequent calls of pthread_once() with the same once_control shall not call the init_routine. On return
from pthread_once(), init_routine shall have completed. The once_control parameter shall determine whether the associ-
ated initialization routine has been called.
//第一次调用 pthread_once()的线程会调用初始化例程,这个初始化函数没有参数,当再次调用 pthread_once()的时候,初始化
//例程将不被调用。当 pthread_once()返回的时候,初始化例程就会完成,once_control变量将会决定初始化例程是否已经被调用
The pthread_once() function is not a cancellation point. However, if init_routine is a cancellation point and is can-
celed, the effect on once_control shall be as if pthread_once() was never called.
// pthread_once()不是一个取消点。但是初始化例程是一个取消点,如果它被取消,那么 pthread_once()就好像从未调用过
The constant PTHREAD_ONCE_INIT is defined in the header.
//PTHREAD_ONCE_INIT定义在头文件pthread.h中
The behavior of pthread_once() is undefined if once_control has automatic storage duration or is not initialized by
PTHREAD_ONCE_INIT.
// pthread_once()的行为是未知的,如果once_control没有被初始化
RETURN VALUE
Upon successful completion, pthread_once() shall return zero; otherwise, an error number shall be returned to indicate
the error.
//成功返回0 ,失败返回错误码
ERRORS
The pthread_once() function may fail if:
// pthread_once()在以下情况会失败
EINVAL If either once_control or init_routine is invalid.
//once_control变量或者初始化例程是无效的
The pthread_once() function shall not return an error code of [EINTR].
//不会返回EINTR
四、实例
1、一次性初始化的验证
点击(此处)折叠或打开
- /*DATE: 2015-4-15
- *AUTHOR: DDDDD
- *DESCRIPTION: 一次性初始化
- int pthread_once(pthread_once_t* once_control, void (*init_routine)(void));
- 如果once_control为0,init_routine()就会执行
- pthread_once()成功返回之后,once_control会变为2
- */
- #include "apue.h"
- pthread_once_t once = 2;
- pthread_t tid;
- void thread_init()
- {
- printf("I'm in thread 0x%x\n", tid);
- }
- void *thread_fun1(void *arg)
- {
- tid = pthread_self();
- printf("I'm thread 0x%x\n", tid);
- printf("once is %d\n", once);
- pthread_once(&once, thread_init);
- printf("once is %d\n", once);
- return NULL;
- }
- void *thread_fun2(void *arg)
- {
- sleep(2);
- tid = pthread_self();
- printf("I'm thread 0x%x\n", tid);
- pthread_once(&once, thread_init);
- return NULL;
- }
- int main()
- {
- pthread_t tid1, tid2;
- int err;
- err = pthread_create(&tid1, NULL, thread_fun1, NULL);
- if(err != 0)
- {
- printf("create new thread 1 failed\n");
- return ;
- }
- err = pthread_create(&tid2, NULL, thread_fun2, NULL);
- if(err != 0)
- {
- printf("create new thread 1 failed\n");
- return ;
- }
- pthread_join(tid1, NULL);
- pthread_join(tid2, NULL);
- return 0;
- }
点击(此处)折叠或打开
- /*DATA: 2015-4-20
- *AUTHOR; WJ
- *DESCRIPTION: 使用多线程对一个队列进行增加和减少,增加操作是一个线程,删除操作是一个线程
- *
- */
- #include "apue.h"
- pthread_mutex_t mutex;
- pthread_once_t once = PTHREAD_ONCE_INIT;
- struct queue{
- int len;
- int write_pos;
- int read_pos;
- int data[50];
- };
- //互斥量初始化函数
- void mutex_init()
- {
- int err;
- err = pthread_mutex_init(&mutex, NULL);
- if(err)
- {
- printf("mutex init failed\n");
- return;
- }
- }
- //队列初始化
- struct queue *queue_init()
- {
- struct queue *que;
- //申请内存
- que = (struct queue *)malloc(sizeof(struct queue));
- if(que ==NULL)
- {
- printf("malloc failed\n");
- return;
- }
- //初始化
- que->len = 0;
- que->write_pos = 0;
- que->read_pos = 0;
- return que;
- }
- void queue_destroy(struct queue *que)
- {
- //销毁互斥量和que
- pthread_mutex_destroy(&mutex);
- free(que);
- }
- void *queue_add(void *arg)
- {
- //对互斥量进行一次性初始化
- pthread_once(&once, mutex_init);
- struct queue *que = (struct queue *)arg;
- int buf=0;
- while(buf<50)
- {
- pthread_mutex_lock(&mutex);
- que->data[que->write_pos] = buf;
- que->write_pos ++;
- que->len ++;
- buf++;
- printf("write data %d to queue\n", que->data[que->write_pos -1]);
- pthread_mutex_unlock(&mutex);
- sleep(1);
- }
- }
- void *queue_del(void *arg)
- {
- // 对互斥量进行一次性初始化
- pthread_once(&once, mutex_init);
- struct queue *que = (struct queue *)arg;
- int buf=0;
- while(1)
- {
- sleep(2);
- pthread_mutex_lock(&mutex);
- buf = que->data[que->read_pos];
- que->read_pos ++;
- if(que->len -- == 0)
- {
- printf("queue is empty\n");
- return;
- }
- buf++;
- printf("read data %d from queue\n", que->data[que->read_pos -1]);
- pthread_mutex_unlock(&mutex);
- }
- }
- int main()
- {
- pthread_t tid1, tid2;
- int err;
- struct queue *que;
- //队列初始化
- que = queue_init();
- err = pthread_create(&tid1, NULL, queue_add, (void *)que);
- if(err)
- {
- printf("create add thread failed\n");
- queue_destroy(que);
- return;
- }
- err = pthread_create(&tid2, NULL, queue_del, (void *)que);
- if(err)
- {
- printf("create del thread failed\n");
- queue_destroy(que);
- return;
- }
- //等待增加和删除操作完成
- pthread_join(tid1, NULL);
- pthread_join(tid2, NULL);
- //销毁
- queue_destroy(que);
- }