问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

请教有关条件变量cond是如何使用的,具体用来干吗的?谢谢

发布网友 发布时间:2022-04-25 20:40

我来回答

2个回答

热心网友 时间:2022-06-17 05:48

mutex是用来保护资源的。
cond是用来通知唤醒的一种机制,如a等待b的输出结果,a可调用pthread_cond_wait来等待,b输出结果后可以通知a,告诉他b的工作已经结束,a可以继续运行了。b调用thread_cond_signal or pthread_cond_broadcas来完成此操作。signal唤醒一个等待者,broadcst唤醒全部。

热心网友 时间:2022-06-17 05:49

这个问题在UNIX高级环境编程中有深刻的讲解,我这里是从英文版中摘抄过来的,你可以用心看一下 :)

Condition variables are another synchronization mechanism available to threads. Condition variables provide a place for threads to rendezvous. When used with mutexes, condition variables allow threads to wait in a race-free way for arbitrary conditions to occur.

The condition itself is protected by a mutex. A thread must first lock the mutex to change the condition state. Other threads will not notice the change until they acquire the mutex, because the mutex must be locked to be able to evaluate the condition.

Before a condition variable is used, it must first be initialized. A condition variable, represented by the pthread_cond_t data type, can be initialized in two ways. We can assign the constant PTHREAD_COND_INITIALIZER to a statically-allocated condition variable, but if the condition variable is allocated dynamically, we can use the pthread_cond_init function to initialize it.

We can use the pthread_mutex_destroy function to deinitialize a condition variable before freeing its underlying memory.

#include <pthread.h>
int pthread_cond_init(pthread_cond_t *restrict cond,
pthread_condattr_t *restrict
attr);

int pthread_cond_destroy(pthread_cond_t *cond);

Both return: 0 if OK, error number on failure

Unless you need to create a conditional variable with nondefault attributes, the attr argument to pthread_cond_init can be set to NULL. We will discuss condition variable attributes in Section 12.4.

We use pthread_cond_wait to wait for a condition to be true. A variant is provided to return an error code if the condition hasn't been satisfied in the specified amount of time.

#include <pthread.h>
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,
const struct timespec *restrict timeout);

Both return: 0 if OK, error number on failure

The mutex passed to pthread_cond_wait protects the condition. The caller passes it locked to the function, which then atomically places the calling thread on the list of threads waiting for the condition and unlocks the mutex. This closes the window between the time that the condition is checked and the time that the thread goes to sleep waiting for the condition to change, so that the thread doesn't miss a change in the condition. When pthread_cond_wait returns, the mutex is again locked.
If the timeout expires without the condition occurring, pthread_cond_timedwait will reacquire the mutex and return the error ETIMEDOUT. When it returns from a successful call to pthread_cond_wait or pthread_cond_timedwait, a thread needs to reevaluate the condition, since another thread might have run and already changed the condition.

There are two functions to notify threads that a condition has been satisfied. The pthread_cond_signal function will wake up one thread waiting on a condition, whereas the pthread_cond_broadcast function will wake up all threads waiting on a condition.

When we call pthread_cond_signal or pthread_cond_broadcast, we are said to be signaling the thread or condition. We have to be careful to signal the threads only after changing the state of the condition.
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
vivox9和x9i有什么区别 ...设置原则跟构造要求。构造柱的设置原则跟构造要求是什么_百度... ...的U型钢筋锚环要几道?在哪里可以找到依据啊 ...月末不含税库存金额1065000元,请计算门店库存天数 公蟹吃什么 公蟹吃什么东西 爬行健身操功能和原理 锻炼爬行的好处有哪些呢 爬行健身法有什么优缺点 檀木如何粘合 成田剑先生还为哪些日本动漫配音,有名一点滴。谢谢了,大神帮忙啊 如何让linux 2.6.18-274版本 支持timerfd linux内核中mdelay怎么实现 time.h头文件实现代码 nanosleep函数 原型是什么 它是以什么单位来计算的(如:毫秒,微妙,纳秒等) c语言代码求助,高手请进。 什么是节点?在linux内核中如何实现的? apue图4-21程序错误提示incompatible types when assigning to type struct timespec fom type _time_t VxWorks 中timespec这个结构体,如果用clock_gettime()来获取tv_sec,tv_nsec,得到的数值代表什么意思啊? redefinition of ‘struct timespec’ 该怎么解决 佳能什么镜头适合拍集体照 一般用单反相机拍50人左右的集体照应该用什么镜头比较好?! 拍集体照用什么档 单反相机拍集体相,用什么档拍好 单反拍集体照要怎么拍 请问我想买一台单反用来拍摄婚礼摄像和学生毕业集体照,请推荐机型和镜头配置,谢谢! 拍一百多人的集体照用什么相机和镜头好 拍摄70人左右的集体照用什么镜头比较好?大概用多少的焦距? 单反相机拍70人的合影! 拍集体照单反怎么设置好? 亲子鉴定结果中说排除同卵多胞胎,近亲,及外源干扰的前提下,支持被检父亲是孩子的生物学父亲? DNA鉴定父子关系:能确认吗?会不会父子鉴定会不是父子吗? 关于结构体数组赋值的问题 udelay在在linux中都有哪些功能 广角镜头和普通镜头的差别 无线蓝牙发射器有哪些用途? 车载蓝牙发射器怎么用 蓝牙音频发射器用多少伏的电压? 蓝牙音频发射器连接蓝牙耳机成功,但耳机没有声音,是什么原因? 为什么手机开蓝牙会很耗电?? 笔记本使用蓝牙需加发射器吗 哪种银行卡用钱免利息的什么卡? 哪个银行的信用卡可以免息提现? 光大银行自选免息分期付款 用什么银行信用卡分期付款可以免利息???我记得在网上听过有!!! 尼康分期免息哪家银行 哪个银行的信用卡分期手续费最低? 哪家银行的信用卡分期手续费最低 用信用卡账单分期能免息吗? 农村箍堂子占用耕地违法吗?在民法典中怎么规定的 配电房电柜着火用什么灭火? 农村箍井的方法