C++ pthread cond_wait 和 cond_broadcast的使用

本文通过一个简单实例展示了pthread_cond_wait和pthread_cond_broadcast的用法。介绍了这两个函数的基本定义及如何在多线程环境中使用它们来同步线程的执行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:https://blog.youkuaiyun.com/wangzhiyu1980/article/details/44227913

参考:

https://blog.youkuaiyun.com/YEYUANGEN/article/details/37593533

https://www.cnblogs.com/secondtonone1/p/5580203.html

 

一个简单的实例程序,说明pthread_cond_wait 和 pthread_cond_broadcast 的使用方式。

 

函数定义:

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)

int pthread_cond_broadcast(pthread_cond_t *cond)

 

函数说明:

pthread_cond_wait :有两个输入参数,一个是pthread_cond_t,是函数将要等待的信号,另一个是 pthread_mutex_t,一个互斥锁。用于对信号量进行保护,防止多个线程同时对其进行操作。在线程开始等待信号量前,必须由本线程对互斥锁进行锁定,然后pthread_cond_wait会更新条件等待队列,并且释放互斥量,允许其他线程进行访问;当cond 满足条件允许线程继续执行时,wait_cond也会先对mutex 进行锁定,对cond进行处理,然后再允许线程继续运行。所以pthread_cond_wait() 后的pthread_mutex_unlock()还是必要的。

 

pic_1

 

 

实例程序:

#include <pthread.h>

#include <iostream>

#include <unistd.h>



using namespace std;


static pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;


static void* func_1(void* arg)

{

cout << "func_1 start" << endl;


pthread_mutex_lock(&mtx);

cout << "func_1 lock mtx" << endl;


cout << "func_1 wait cond" << endl;

pthread_cond_wait(&cond, &mtx);


cout << "func_1 unlock mtx" << endl;

pthread_mutex_unlock(&mtx);


cout << "func_1 end" << endl;

sleep(5);


return NULL;

}


static void* func_2(void* arg)

{

cout << "func_2 start" << endl;


pthread_mutex_lock(&mtx);

cout << "func_2 lock mtx" << endl;


cout << "func_2 wait cond" << endl;

pthread_cond_wait(&cond, &mtx);


cout << "func_2 unlock mtx" << endl;

pthread_mutex_unlock(&mtx);


cout << "func_2 end" << endl;

sleep(5);


return NULL;

}



int main()

{

pthread_t tid1, tid2;


cout << "main create thread" << endl;

pthread_create(&tid1, NULL, func_1, NULL);

pthread_create(&tid2, NULL, func_2, NULL);


sleep(3);

cout << "main boradcast signal" << endl;

pthread_cond_broadcast(&cond);


cout << "main join thread" << endl;


pthread_join(tid1, NULL);

pthread_join(tid2, NULL);


cout << "main end" << endl;

return 0;

}


 

测试结果:

pic_1

pthread_cond_broadcastpthread_cond_wait也是用于线程间同步的函数,类似于pthread_cond_signalpthread_cond_wait的组合,但有一些区别。 pthread_cond_broadcast用于广播条件变量的信号。当一个线程调用pthread_cond_broadcast时,它会唤醒所有正在等待这个条件变量的线程。这与pthread_cond_signal的区别在于,pthread_cond_signal只会唤醒一个等待线程,而pthread_cond_broadcast会唤醒所有等待线程。 pthread_cond_wait用于等待条件变量的信号,与pthread_cond_signalpthread_cond_broadcast一起使用。当一个线程调用pthread_cond_wait时,它会阻塞等待条件变量的信号。当收到信号后,线程会重新激活,并且会重新检查条件是否满足。如果条件不满足,线程可能会再次进入等待状态。 以下是一个使用pthread_cond_broadcastpthread_cond_wait的示例代码: ```c pthread_mutex_t mutex; pthread_cond_t cond; int condition = 0; void* thread1(void* arg) { pthread_mutex_lock(&mutex); while (condition == 0) { pthread_cond_wait(&cond, &mutex); } // 条件满足后执行的代码 pthread_mutex_unlock(&mutex); return NULL; } void* thread2(void* arg) { pthread_mutex_lock(&mutex); condition = 1; pthread_cond_broadcast(&cond); pthread_mutex_unlock(&mutex); return NULL; } int main() { pthread_t tid1, tid2; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); pthread_create(&tid1, NULL, thread1, NULL); pthread_create(&tid2, NULL, thread2, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); return 0; } ``` 在上述示例中,thread1线程调用pthread_cond_wait等待条件满足,而thread2线程在某个时刻将条件设置为满足,并调用pthread_cond_broadcast发送信号。这样,所有等待的线程都会被唤醒并执行相应的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值