现在的位置: 首页 > 综合 > 正文

pthread_cond_wait和pthread_cond_timedwait学习

2013年10月09日 ⁄ 综合 ⁄ 共 2671字 ⁄ 字号 评论关闭

语义

 #include <pthread.h>
 int pthread_cond_wait(pthread_cond_t *cond,
                      pthread_mutex_t *mutex);   

  Service Program Name: QP0WPTHR

  Default Public Authority: *USE 

线程安全

非信号安全


两个函数在条件上等待。

描述

pthread_cond_wait() 和 pthread_cond_timedwait()都被用来在条件变量上阻塞。他们被调用线程用锁住的变量来调用,否则会出现未定义结果。

这些函数自动释放mutex,并引起调用线程在条件变量上阻塞;“自动”在这里意味着“另一线程可以获取mutex,然后是条件变量。”也就是说,如果另一线程在当前阻塞线程释放它后能够获得muext,然后在本线程内顺序调用pthread_cond_signal() 或者 pthread_cond_broadcast()

如果函数成功返回,mutex已经锁住,被调用线程拥有。

When using condition variables there is always a boolean predicate involving shared variables associated with each condition wait that is true if the thread should proceed. Spurious wakeups from the pthread_cond_wait() or pthread_cond_timedwait() functions
may occur. Since the return from pthread_cond_wait() orpthread_cond_timedwait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.

当用条件变量

The effect of using more than one mutex for concurrent pthread_cond_wait() or pthread_cond_timedwait() operations on the same condition variable is undefined; that is, a condition variable becomes bound to a unique mutex when a thread waits
on the condition variable, and this (dynamic) binding ends when the wait returns.

A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set to PTHREAD_CANCEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is (in effect)
re-acquired before calling the first cancellation cleanup handler. The effect is as if the thread were unblocked, allowed to execute up to the point of returning from the call to pthread_cond_wait() or pthread_cond_timedwait(), but at that
point notices the cancellation request and instead of returning to the caller of pthread_cond_wait() or pthread_cond_timedwait(), starts the thread cancellation activities, which includes calling cancellation cleanup handlers.

A thread that has been unblocked because it has been canceled while blocked in a call to pthread_cond_wait() or pthread_cond_timedwait() does not consume any condition signal that may be directed concurrently at the condition variable if
there are other threads blocked on the condition variable.

The pthread_cond_timedwait() function is the same as pthread_cond_wait() except that an error is returned if the absolute time specified by abstime passes (that is, system time equals or exceeds abstime) before the condition cond is
signaled or broadcasted, or if the absolute time specified by abstime has already been passed at the time of the call. When such time-outs occur, pthread_cond_timedwait() will nonetheless release and reacquire the mutex referenced by mutex.
The functionpthread_cond_timedwait() is also a cancellation point.

If a signal is delivered to a thread waiting for a condition variable, upon return from the signal handler the thread resumes waiting for the condition variable as if it was not interrupted, or it returns zero due to spurious wakeup.

抱歉!评论已关闭.