pthread_cond_wait

来源:互联网 发布:plc为什么比单片机稳定 编辑:程序博客网 时间:2024/06/07 06:09

官方说明参见:

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩和家长一吵架就说死怎么办 王者荣耀号被别人家长联接了怎么办 儿子12岁总是跟大人顶嘴怎么办 课堂上有学生和你顶嘴你怎么办 小孩看到大人吵架就哭了 怎么办 五年级的孩子叛逆爱发脾气怎么办 6个月孩子多动怎么办呀 小孩被大人打了很生气该怎么办 小孩在学校调皮被老师打了怎么办 四岁宝宝咳嗽半个月了怎么办 三岁宝宝咳嗽半个月了还不好怎么办 孩子在幼儿园不听话天天罚站怎么办 孩子个性太强脾气太倔不听话怎么办 孩子有十七了太不听话了怎么办 小孩的学籍填错了怎么办还能改吗 生完孩子后脾气暴躁易怒怎么办 生完孩子之后变懒了怎么办呀? 孩子三年级了成绩不突出家长怎么办 自私势力的父母想把我害死怎么办 父母如果养出自私的孩子怎么办 大人得了地图舌怎么办要怎么治疗 请问我家小孩有心理儿问题怎么办 想离婚妻子拿孩子命威胁怎么办 爸妈50多岁了吵架很严重怎么办 我和别人没离婚的老婆在一起怎么办 老婆起诉我离婚我不想理怎么办 上大学的孩子对考试无所谓怎么办 家里的人对我已经没有信心了怎么办 我是做股票配资的找不到客户怎么办 心理健康类的课程总是选不了怎么办 孩子该上初三了英语基础差怎么办 初二学生辍学半年又想上学怎么办 保险用的小红本丢了怎么办 孩子的小红本丢了怎么办 宝宝打针的小红本丢了怎么办 居民养老保险小红本丢了怎么办 小学六年级素质报告单丢了怎么办 西安小学素质报告册有b怎么办 辽宁省干部在线学习时长不够怎么办 鬼火一代发动机在中间卡死怎么办 医疗权与患者隐私权想冲突怎么办