pthread

    科技2024-11-19  26

    文章目录

    1、函数原型:2、pthread_once_t、PTHREAD_ONCE_INIT定义3、代码实例:4、应用 & 参考

    1、函数原型:

    int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));

    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.

    2、pthread_once_t、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__ 8

    PTHREAD_ONCE_INIT的定义

    #define PTHREAD_ONCE_INIT {_PTHREAD_ONCE_SIG_init, {0}} #define _PTHREAD_ONCE_SIG_init 0x30B1BCBA

    3、代码实例:

    /* donald: pthread_once中的thread_init只会执行一次,在多线程中具体哪一个线程执行不一定 DESCRIPTION: 一次性初始化 int pthread_once(pthread_once_t* once_control, void (*init_routine)(void)); 如果once_control为0,init_routine()就会执行 pthread_once()成功返回之后,once_control会变为2 */ #include <iostream> #include <pthread.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <errno.h> pthread_once_t once = PTHREAD_ONCE_INIT; pthread_t tid; void thread_init() { printf("init::I'm in thread 0x%x\n", tid); } void *thread_fun1(void *arg) { tid = pthread_self(); printf("th1:I'm thread 0x%x\n", tid); printf("th1:once is %d\n", once); pthread_once(&once, thread_init); printf("th1:once is %d\n", once); return NULL; } void *thread_fun2(void *arg) { // sleep(2); tid = pthread_self(); printf("th2:I'm thread 0x%x\n", tid); pthread_once(&once, thread_init); printf("th2:once is %d\n", once); printf("th2:once is %d\n", once); return NULL; } int main() { pthread_t tid1, tid2; int err; printf("main:once is %lu\n", once); // std::cout << "main:once is " << once << std::endl; err = pthread_create(&tid1, NULL, thread_fun1, NULL); if(err != 0) { printf("create new thread 1 failed\n"); return 0; } err = pthread_create(&tid2, NULL, thread_fun2, NULL); if(err != 0) { printf("create new thread 1 failed\n"); return 0; } printf("I'm main thread 0x%x, tid1:0x%x, tid2:0x%x\n", pthread_self(), tid1, tid2); pthread_join(tid1, NULL); pthread_join(tid2, NULL); return 0; }

    运行结果:

    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值也未改变。

    4、应用 & 参考

    **应用:**可以用pthread_once来实现一个单例模式,具体实现见另一篇博文【单例模式实现方法】

    参考:

    【pthread_once()使用】【线程高级:一次性初始化】pthread_once实现单例模式,《Linux多线程服务端编程》陈硕,P49
    Processed: 0.013, SQL: 8