关于pthread_cond_wait()函数的理解

关于pthread_cond_wait()函数的理解
1、pthread_cond_wait()在等待条件变量时,会主动释放锁并且挂起当前线程或进程;
   这是Linux man-page上原话:
   These functions(pthread_cond_wait, pthread_cond_timedwait) aotomitically release muetex and cause the calling thread 
to block on the condition variable cond.
2、pthread_cond_wait如果成功返回,将自动获取先前释放的锁
    这是Linux man-page上原话:
    Upon succssful return, the mutex shall have been locked and shall be owned by the calling thread.

3、pthread_cond_wait总是与一个布尔判断式相联在一起,如果判断式为真,则线程继续执行,然而pthread_cond_wait返回时无法保证判断式是真是假,因此需要重新判断。

   When using condition variables there is always a Boolean predicate involving shared variables associated with 
each condtion wait that is ture if the thread should proceed...
Since the return does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.

4、鉴于第三点,强烈建议pthread_cond_wait与while循环结合使用来检查判断式

   It is thus recommened that a condition wait be enclosed in the equivalent of a "while loop" that checks the predicate.

你可能感兴趣的:(关于pthread_cond_wait()函数的理解)