LINUX条件锁的使用
来源:互联网 发布:ubuntu vnc 灰屏 编辑:程序博客网 时间:2024/05/08 15:03
LINUX环境下多线程编程肯定会遇到需要条件变量的情况,此时必然要使用pthread_cond_wait()函数。但这个函数的执行过程比较难于理解。
和:
引用下POSIX的RATIONALE:
Condition Wait Semantics
It is important to note that when pthread_cond_wait() and pthread_cond_timedwait() return without error, the associated predicate may still be false. Similarly, when pthread_cond_timedwait() returns with the timeout error, the associated predicate may be true due to an unavoidable race between the expiration of the timeout and the predicate state change.
The application needs to recheck the predicate on any return because it cannot be sure there is another thread waiting on the thread to handle the signal, and if there is not then the signal is lost. The burden is on the application to check the predicate.
Some implementations, particularly on a multi-processor, may sometimes cause multiple threads to wake up when the condition variable is signaled simultaneously on different processors.
In general, whenever a condition wait returns, the thread has to re-evaluate the predicate associated with the condition wait to determine whether it can safely proceed, should wait again, or should declare a timeout. A return from the wait does not imply that the associated predicate is either true or false.
It is thus recommended that a condition wait be enclosed in the equivalent of a "while loop" that checks the predicate.
从上文可以看出:
1,pthread_cond_signal在多处理器上可能同时唤醒多个线程,当你只能让一个线程处理某个任务时,其它被唤醒的线程就需要继续 wait,while循环的意义就体现在这里了,而且规范要求pthread_cond_signal至少唤醒一个pthread_cond_wait上 的线程,其实有些实现为了简单在单处理器上也会唤醒多个线程.
2,某些应用,如线程池,pthread_cond_broadcast唤醒全部线程,但我们通常只需要一部分线程去做执行任务,所以其它的线程需要继续wait.所以强烈推荐此处使用while循环.
- LINUX条件锁的使用
- LINUX条件锁的使用
- Linux条件变量的使用
- 条件锁的使用
- linux下条件变量的使用
- linux下互斥锁和条件变量的使用
- Linux 条件变量的使用pthread_cond
- linux中互斥量与条件变量的使用
- linux 线程的条件锁
- linux条件锁pthread_cond_t使用示例
- 条件锁(条件Mutex)pthread_cond_wait、pthread_cond_signal、pthread_cond_broadcast的使用
- 条件锁(条件Mutex)pthread_cond_wait、pthread_cond_signal、pthread_cond_broadcast的使用
- Linux下面的线程锁,条件变量以及信号量的使用
- Linux下面的线程锁,条件变量以及信号量的使用
- Linux下面的线程锁,条件变量以及信号量的使用
- Linux下面的线程锁,条件变量以及信号量的使用
- Linux下面的线程锁,条件变量以及信号量的使用
- Linux下面的线程锁,条件变量以及信号量的使用
- 使用nodejs创建一个网站一
- c++ 4种类型转换
- Javascript 中的引用类型(2)—— 理解对象
- PHP 抽象类
- jquery选择器之伪类选择器
- LINUX条件锁的使用
- 在jsp页面,如何让一个button具有submit的功能
- oracle11g-rac重建CRS(grid)
- 【ios开发】多语言环境
- qwt移植到arm开发板
- MongoDB设置访问权限、设置用户
- Unity3D开发之常用笔记
- 运行dxp.exe运行时,软件出现:应用程序发生异常 未知的软件异常 (0x0eedfade),位置为 0x7c812afb的处理方法
- 解決Button的OnClientClick和RequiredFieldValidator的冲突问题