pthread_once的作用为,在给定once_control的情况下,在整个程序中仅调用一次init_routine(在多线程中具体哪一个线程执行不一定)。如果再次调用,pthread_once将不会调用init_routine。
该函数执行成功返回0,执行失败返回错误码。
具体可以参考下如下man手册:
The first call to pthread_once() by any thread in a process, with a given once_control, will call the init_routine() with no arguments.Subsequent calls to pthread_once() with the same once_control will not call the init_routine(). On return from pthread_once(), it is guaranteed that init_routine() has completed. The once_control parameter is used to determine whether the associated initialization routine has been called.以下说明了once_control需要初始化,如果初值不是PTHREAD_ONCE_INIT,pthread_once行为就不正常:
The function pthread_once() is not a cancellation point. However, if init_routine() is a cancellation point and is cancelled, the effect on once_control is as if pthread_once() was never called.The constant PTHREAD_ONCE_INIT is defined by header <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_t变量的定义:
typedef __darwin_pthread_once_t pthread_once_t; typedef struct _opaque_pthread_once_t __darwin_pthread_once_t; struct _opaque_pthread_once_t { long __sig; char __opaque[__PTHREAD_ONCE_SIZE__]; }; #define __PTHREAD_ONCE_SIZE__ 8PTHREAD_ONCE_INIT的定义
#define PTHREAD_ONCE_INIT {_PTHREAD_ONCE_SIG_init, {0}} #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA运行结果:
main:once is 816954554 I'm main thread 0xb28ddc0, tid1:0xf8c3000, tid2:0xf946000 th1:I'm thread 0xf8c3000 th1:once is 816954554 th2:I'm thread 0xf946000 init::I'm in thread 0xf946000 th1:once is 1330529093 th2:once is 1330529093 th2:once is 1330529093结果释义:
在main中输出once,值为816954554,816954554实际上是0x30B1BCBA(16进制),根据上面PTHREAD_ONCE_INIT的定义,可以知道这里0x30B1BCBA就是其初始值。
根据输出的内容可以看出,thread_init只运行了一次(输出init::I’m in thread 0xf946000),之后的once值也未改变。
**应用:**可以用pthread_once来实现一个单例模式,具体实现见另一篇博文【单例模式实现方法】
参考:
【pthread_once()使用】【线程高级:一次性初始化】pthread_once实现单例模式,《Linux多线程服务端编程》陈硕,P49