关闭

pthread_cond_wait

491人阅读 评论(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
查看评论

pthread_cond_wait的一个例子

关于条件变量的一个例子,其中的重点是: 1.pthread_cond_wait会先将调用线程放到等待条件的线程列表上,也就是说线程会阻塞,传递进去的互斥量会解锁; 2.pthread_cond_wait返回时互斥量会再次锁住。 带着这两个要点去看下面的程序,thread1负责打印i的值,thr...
  • jimmy_w
  • jimmy_w
  • 2014-08-14 23:26
  • 1353

pthread_cond_wait前面使用while()问题

概念:   假设实现一个生产者消费者模型,仓库只能装100万件item,生产者每次生产1件,消费者每次消费1件,为了保持数据的同步,那么生产者每次都需要检查仓库是否满了,消费者每次都需要检查仓库是否空了,在加上互斥锁前提下,数据是能得同步保证的。循环的访问且每次都给互斥锁加锁解锁,又称为轮...
  • guozhiyingguo
  • guozhiyingguo
  • 2017-01-16 11:21
  • 489

Linux中posix线程的pthread_cond_wait函数为何使用while循环

关于pthread_cond_wait函数为何需要使用while循环,或者为何不使用if条件判断问题,经过Google后发现网上有不少已说明,这里给出自己的说明: 该函数的定义,可参考http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_c...
  • sinat_15799399
  • sinat_15799399
  • 2015-01-06 20:55
  • 1042

互斥锁pthread_mutex_t与pthread_cond_wait的使用

http://blog.chinaunix.net/uid-9543173-id-3579371.html 一、互斥锁的使用 1、互斥锁创建 有两种方法创建互斥锁,静态方式和动态方式。 a)、POSIX定义了一个宏PTHREAD_MUTEX_INITIALIZER来静态初始化互斥锁,方法如下...
  • bytxl
  • bytxl
  • 2013-04-19 10:16
  • 1176

互斥量、条件变量与pthread_cond_wait()函数的使用,详解(二)

1.Linux“线程”      进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型。Linux是一种“多进程单线程”的操作系统。Linux本身只有进程的概念,而其所谓的“线程”本质上在内核里仍然是进程。  ...
  • cnclenovo
  • cnclenovo
  • 2015-03-24 11:30
  • 4166

关于pthread_cond_wait使用while循环判断的理解

在Stevens的《Unix 环境高级编程》中第11章线程关于pthread_cond_wait的介绍中有一个生产者-消费者的例子P311, 在进入pthread_cond_wait前使用while进行条件判断,而没有直接使用if,耐人费解! #include struct msg { ...
  • ywy2090
  • ywy2090
  • 2013-10-29 18:21
  • 845

条件变量 pthread_cond_wait 函数的解锁和等待为原子操作

看网上的示例代码,总是看不懂,总是觉得哪里不对
  • holdsky
  • holdsky
  • 2014-05-22 11:48
  • 1607

互斥量、条件变量与pthread_cond_wait()函数的使用,详解(一)

1. 首先pthread_cond_wait 的定义是这样的 The pthread_cond_wait() and pthread_cond_timedwait() functions are used to block on a condition varia...
  • cnclenovo
  • cnclenovo
  • 2015-03-24 11:24
  • 902

基于条件变量阻塞pthread_cond_wait

基于条件变量阻塞 使用 pthread_cond_wait(3C) 可以以原子方式释放 mp 所指向的互斥锁,并导致调用线程基于 cv 所指向的条件变量阻塞。对于 Solaris 线程,请参见cond_wait 语法。 p...
  • a1232345
  • a1232345
  • 2015-01-27 14:41
  • 549

为什么pthread_cond_wait需要互斥锁为参数

呃。。本人新人,下面只是我的猜想,如果有大神指错就更好了     先必须知道如下:     对于多线程条件变量,其pthread_cond_wait对同一个条件变量值能有一个有效。因为如果有多个,就会引起race condition(想想也...
  • huimiezhex
  • huimiezhex
  • 2015-09-11 12:55
  • 156
    个人资料
    • 访问:101914次
    • 积分:2150
    • 等级:
    • 排名:千里之外
    • 原创:101篇
    • 转载:7篇
    • 译文:16篇
    • 评论:8条
    最新评论