一、为什么要使用一次性初始化
有些事需要且只能执行一次(比如互斥量初始化)。通常当初始化应用程序时,可以比较容易地将其放在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。四、实例
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;
}
2、将互斥量的初始化,使用pthread_once来实现
/*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);
}