关闭

pthread_cond_wait

303人阅读 评论(0) 收藏 举报

官方说明参见:

http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_wait.html

NAME

pthread_cond_wait, pthread_cond_timedwait - wait on a condition

 SYNOPSIS



#include <pthread.h>

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_timedwait(pthread_cond_t *cond, 
    pthread_mutex_t *mutex, const struct timespec *abstime);

 DESCRIPTION

    函数pthread_cond_wait()和pthread_cond_timewait()用于基于条件变量进行线程阻塞。它们被即将被调用的线程或尚未定义的行为以一个锁住的mutex变量为参数进行调用。这两个函数会自动释放mutex并使调用它的线程阻塞在条件变量上(直到条件满足),此处的自动意味着"主动地让其他线程访问mutex变量和条件变量".也就是说,如果另一个线程可以在准备阻塞的线程释放mutex变量之后访问mutex变量,那么这个线程中随后调用的pthread_cond_signal()和pthread_cond_broadcast()会在准备阻塞的线程阻塞之后表现得可以分配到这个锁。
如果调用成功,mutex变量将会被锁住并被调用线程拥有。
使用条件变量的时候通常会有一个和各个条件变量关联的共享变量的boolean型断言,调用线程可以继续时断言为true。
欺骗性的唤醒可能从pthread_cond_wait()或者pthread_cond_timedwait()中产生。由于pthread_cond_wait()或pthread_cond_timedwait()并不体现任何断言的值,断言需要基于这个返回值进行重复判断。

The pthread_cond_wait() and pthread_cond_timedwait() functions are used to block on a condition variable. They are called with mutex locked by the calling thread or undefined behaviour will result.

These functions atomically release mutex and cause the calling thread to block on the condition variable cond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". That is, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call to pthread_cond_signal() or pthread_cond_broadcast() in that thread behaves as if it were issued after the about-to-block thread has blocked.

Upon successful return, the mutex has been locked and is owned by the calling thread.

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() orpthread_cond_timedwait() functions may occur.  the return from pthread_cond_wait() or pthread_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 topthread_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 conditioncond 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 function pthread_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.

 RETURN VALUE

Except in the case of [ETIMEDOUT], all these error checks act as if they were performed immediately at the beginning of processing for the function and cause an error return, in effect, prior to modifying the state of the mutex specified by mutex or the condition variable specified by cond.

Upon successful completion, a value of zero is returned. Otherwise, an error number is returned to indicate the error.

 ERRORS

The pthread_cond_timedwait() function will fail if:
[ETIMEDOUT]
The time specified by abstime to pthread_cond_timedwait() has passed.

The pthread_cond_wait() and pthread_cond_timedwait() functions may fail if:

[EINVAL]
The value specified by cond, mutex, or abstime is invalid.
[EINVAL]
Different mutexes were supplied for concurrent pthread_cond_wait() or pthread_cond_timedwait() operations on the same condition variable.
[EINVAL]
The mutex was not owned by the current thread at the time of the call.

These functions will not return an error code of [EINTR].

 EXAMPLES

None.

 APPLICATION USAGE

None.

 FUTURE DIRECTIONS

None.

 SEE ALSO

pthread_cond_signal()pthread_cond_broadcast()<pthread.h>.

DERIVATION

Derived from the POSIX Threads Extension (1003.1c-1995)

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:72913次
    • 积分:1853
    • 等级:
    • 排名:千里之外
    • 原创:101篇
    • 转载:7篇
    • 译文:16篇
    • 评论:7条
    最新评论