Linux系统编程——线程(四)条件相关API

条件变量是线程另一可用的同步机制。条件变量给多个线程提供了一个会合的场所。条件变量与互斥量一起使用时,允许线程以无竞争的方式等待特定的条件发生。

条件本身是由互斥量保护的。线程在改变条件状态前必须首先锁住互斥量,其他线程在获得互斥量之前不会察觉到这种改变,因为必须锁定互斥量以后才能计算条件。

条件变量使用之前必须首先初始化

pthread_cond_t数据类型代表的条件变量可以用两种方式进行初始化,可以把常量PTHREAD_COND_INITIALIZER赋给静态分配的条件变量

静态初始化:pthread_cond_t cond = PTHREAD_COND_INITIALIZER

动态初始化: pthread_cond_init(&cond,NULL);

如果条件变量是动态分配的,可以使用pthread_cond_destroy函数对条件变量进行去除初始化。

条件有 创建、销毁,触发,广播,和等待5种操作

所有 API 的头文件

#include <pthread.h>

1、 创建和销毁条件变量

函数原型

//创建
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t *cond);//销毁
// 返回:若成功返回0,否则返回错误编号

两个参数都是pthread_cond_t类型的指针

如果需要创建一个非默认属性的条件变量,则pthread_cont_init函数的attr参数设置为NULL。

2、等待

函数原型

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);
// 返回:若成功返回0,否则返回错误编号

pthread_cond_wait等待条件变为真。如果在给定的时间内条件不能满足,那么会生成一个代表一个出错码的返回变量。传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传给函数。

pthread_cond_timedwait函数的工作方式与pthread_cond_wait函数类似,只是多了一个timeout=。timeout指定了等待的时间,它是通过timespec结构指定。

3、触发和广播

触发表示唤醒任意一个等待该条件的线程,而广播表示唤醒所有等待该条件的线程。

函数原型

//触发
int pthread_cond_signal(pthread_cond_t cond);
//广播
int pthread_cond_broadcast(pthread_cond_t cond);
// 返回:若成功返回0,否则返回错误编号

这两个函数可以用于通知线程条件已经满足。pthread_cond_signal函数将唤醒等待该条件的某个线程,而pthread_cond_broadcast函数将唤醒等待该条件的所有进程

注意:一定要在改变条件状态以后再给线程发信号。

4、示例代码

#include <stdio.h>
#include <pthread.h>

int p_data = 0;
pthread_mutex_t mutex; //定义互斥量
pthread_cond_t cond; //定义条件变量

//就算是线程一先运行,(线程一会等待)
//因为它没有互斥锁,所以也会被线程二竞争过去,线程二有锁
void *func1(void *arg)
{
    while(1){
        pthread_cond_wait(&cond,&mutex); //等待条件满足
        printf("t1 p_data : %d\n",p_data++); //满足条件后,输出p_data
        printf("=========================\n");
        p_data = 0;    //复位p_data,方便下一次使用
        sleep(1);
    }
}

void *func2(void *arg)
{
    while(1){
        pthread_mutex_lock(&mutex); //加锁
        printf("t2 p_data : %d\n",p_data++); //打印值
        if(p_data == 5){ //是否满足条件
        	pthread_cond_signal(&cond); //通知线程一条件已经满足
        }
        pthread_mutex_unlock(&mutex);  //解锁
        sleep(1);
    }
}
int main()
{
    int ret;
    int param = 100;
    pthread_t t1; //线程一
    pthread_t t2; //线程二
    pthread_mutex_init(&mutex,NULL); //创建互斥量
    pthread_cond_init(&cond,NULL);   //创建条件量
    ret = pthread_create(&t1,NULL,func1,(void *)&param);
    ret = pthread_create(&t2,NULL,func2,(void *)&param);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    pthread_mutex_destroy(&mutex); //销毁互斥量
    pthread_cond_destroy(&cond);   //销毁条件量
    return 0;
}

运行结果

在这里插入图片描述
从运行结果可以看出,刚开始一直是线程二在运行,到了p_data = 5 时,线程二通知线程一条件满足,所以线程一结束等待,打印了p_data的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值