cond

cond,第1张

cond 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 <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.回答者:jinglebaby0807 - 助理 三级 11-6 22:05提问者对于答案的评价:我会好好看的,谢谢评价已经被关闭 目前有 1 个人评价 好100% (1) 不好0% (0) 相关内容?? 头都大了,跪求ASP中一个判断语句 查看同主题问题:cond 干吗 其他回答 共 1 条mutex是用来保护资源的。

cond是用来通知唤醒的一种机制,如a等待b的输出结果,a可调用pthread_cond_wait来等待,b输出结果后可以通知a,告诉他b的工作已经结束,a可以继续运行了。

b调用thread_cond_signal or pthread_cond_broadcas来完成此 *** 作。

signal唤醒一个等待者,broadcst唤醒全部。

17-4PHCOND.H900是什么材料

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/3933209.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-20
下一篇 2022-10-20

发表评论

登录后才能评论

评论列表(0条)

保存