永发信息网

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

答案:2  悬赏:70  手机版
解决时间 2021-04-03 01:11
请教有关条件变量cond是如何使用的,具体用来干吗的?谢谢
最佳答案
这个问题在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
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
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.
全部回答
mutex是用来保护资源的。
cond是用来通知唤醒的一种机制,如a等待b的输出结果,a可调用pthread_cond_wait来等待,b输出结果后可以通知a,告诉他b的工作已经结束,a可以继续运行了。b调用thread_cond_signal or pthread_cond_broadcas来完成此操作。signal唤醒一个等待者,broadcst唤醒全部。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
模式的模的拼音
system.runtime需要什么包
关于 close 和 near 。 有何明显的区别?
今年24岁,输了28万外债还欠20万,我该怎么办
为什么真空冷面不需要泡就可以煮,而自己在市
今年为什么不流行魔术了?
高压电工比低压易找工作吗?
乘坐滴滴打车和滴答打车怎样保护自己
无遮无拦是什么意思?
江苏常熟传销多吗?我去常熟怕被骗。常熟传销
禹竺这个名字好吗?
单选题下列化合物中所含氧族元素原子的化合价
特别初一和十五这天出生的小孩更难带一些
刚刚租了房子,墙太脏了,怎么办?
谁有Visio_Premium_2010w_SP1_W32的激活码,
推荐资讯
如何寻找“昨日重现”形态之个股
正统三国过第二十八章怎么过
为什么明明不想那样做一些事,还不由自主就做
lol永猎双子千珏头像在哪儿领取
禹州招聘有夜班工作吗
10,12,这个数列的第25个数是多少
五帝钱跟金木水火土
通常空气是无色无味的气体看不见摸不着.你能
什么女人在男人眼里很好泡
想从水路运输东西到湖南长沙
鼻涕往嘴里流是什么原因
神墨学生服务社地址在什么地方,想过去办事,
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?