pthread_once_t 类型 和 pthread_once 函数


author: hjjdebug
date: 2025年 03月 05日 星期三 09:44:42 CST
description: pthread_once_t 类型 和 pthread_once 函数



pthread_once_t 和pthread_once 这两个单词在编程时看着是比较陌生的,
它们都与多线程编程有关
下面先给一个简单的测试代码.

1. 测试代码

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
//在pthreadtypes.h 中有定义 typedef int pthread_once_t;
//pthread.h 中有定义 #define PTHREAD_ONCE_INIT 0
// 下面这条神秘的语句就是 普通的 int once = 0;
pthread_once_t once = PTHREAD_ONCE_INIT;
void init_routine() {
    printf("Initialization performed\n");
}

void* thread_func(void* arg) {
	//关键是pthread_once 库函数, 它能够保证只调用一次init_routine,不管有多少次运行thread_func
    pthread_once(&once, init_routine); // 确保init_routine只执行一次
	printf("once:%d\n",once);
    printf("Thread %ld finished execution\n", (long)arg);
    return NULL;
}

int main() {
    pthread_t t1, t2,t3;
    pthread_create(&t1, NULL, thread_func, (void*)1);
    pthread_create(&t2, NULL, thread_func, (void*)2);
    pthread_create(&t3, NULL, thread_func, (void*)3);
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);
    return 0;
}

2. 运行结果, 达到目的.

$ ./pthread_once
Initialization performed
once:2
Thread 1 finished execution
once:2
Thread 3 finished execution
once:2
Thread 2 finished execution

3. 代码分析

把代码进行预处理后发现, 这两个陌生的单词 pthread_once_t 类型 ,pthread_once 函数
一个是李逵,一个是李鬼.

3.1 pthread_once_t 就是个李鬼, 它就是int 类型

因为它在pthreadtypes.h 中有定义 typedef int pthread_once_t;
就是说,pthread_once_t 就是 int 类型
同时在pthread.h 中有定义 #define PTHREAD_ONCE_INIT 0
这样, 那条看起来神秘的语句
pthread_once_t once = PTHREAD_ONCE_INIT;
实际就是:
int once = 0;
就是一个全局整形变量,赋值为0.

3.2. pthread_once 是libc 的库函数, 如同pthread_create, pthread_join 函数一样.

那么它是怎样做到只调用一次初始化函数init_routine 呢?
纵使不看库实现代码,也可以推断出它的工作过程.
线程函数pthread_once 被调用执行时,
首先它查看传来的全局变量 once 的值,若不为0,则直接返回.不会执行init_routine
若为0,则准备执行传来的函数init_route, 能不能真正执行,还要去竞争一个线程变量mutex
获取了这个线程变量,则执行,否则等待. 这是多线程执行必需要考虑的竞争问题.

获得执行权的线程,先查看变量once是否为0,为0,则执行初始化函数 init_route,并将once flag变量置为真值.
然后释放锁mutex.
如果有其它线程在等待这个锁, 它就获得了这个锁,具有了执行权,
它首先也要判断once是否为0, 由于第一个线程已经把once 标志置位了, 所以就直接释放锁,返回而不会再执行init_routine 函数了.
这样就实现了多个线程同时执行,只执行一次初始化函数的目的.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值