为什么调用pthread_cond_wait有虚假唤醒? [英] Why does pthread_cond_wait have spurious wakeups?

查看:98
本文介绍了为什么调用pthread_cond_wait有虚假唤醒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要引用的手册页:

在使用条件变量始终存在涉及与每个条件等待这是真的如果线程应该进行相关的共享变量布尔predicate。从那么pthread_cond_timedwait()或调用pthread_cond_wait杂散唤醒()可能出现的功能。因为从那么pthread_cond_timedwait()或pthread_cond_wait()的,并不意味着这个predicate的任何有价值的东西回归,predicate应该在这样的回报率重新评估。

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_timedwait() or pthread_cond_wait() functions may occur. Since the return from pthread_cond_timedwait() or pthread_cond_wait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.

因此​​,调用pthread_cond_wait 甚至如果你还没有暗示其返回。乍一看,至少,这似乎是pretty残暴。它会像它随机返回错误的值或者它实际上达到一个适当的回报语句之前随机返回的功能。这似乎是一个重大错误。但是他们选择的事实在手册页记录本,而不是修复它似乎表明有就是为什么调用pthread_cond_wait 结束不合逻辑醒来一个正当的理由。 presumably,有一些内在的它是如何工作,使得它,这样不能帮助。现在的问题是什么。

So, pthread_cond_wait can return even if you haven't signaled it. At first glance at least, that seems pretty atrocious. It would be like a function which randomly returned the wrong value or randomly returned before it actually reached a proper return statement. It seems like a major bug. But the fact that they chose to document this in the man page rather than fix it would seem to indicate that there is a legitimate reason why pthread_cond_wait ends up waking up spuriously. Presumably, there's something intrinsic about how it works that makes it so that that can't be helped. The question is what.

为什么的不调用pthread_cond_wait 返回不合逻辑?为什么不能保证它一定会醒过来,当它被正确信号?谁能解释其寄生行为的原因是什么?

Why does pthread_cond_wait return spuriously? Why can't it guarantee that it's only going to wake up when it's been properly signaled? Can anyone explain the reason for its spurious behavior?

推荐答案

下面的说明是由David R. Butenhof中给出的\"Programming使用POSIX线程(第80页):

The following explanation is given by David R. Butenhof in "Programming with POSIX Threads" (p. 80):

虚假唤醒可能听起来很奇怪,但在一些多处理器系统,使病情完全唤醒predictable可能大幅放缓的所有条件变量的操作。

Spurious wakeups may sound strange, but on some multiprocessor systems, making condition wakeup completely predictable might substantially slow all condition variable operations.

在以下<一href=\"https://groups.google.com/group/comp.programming.threads/browse_frm/thread/327958c427ea978c?hl=de&pli=1\">comp.programming.threads讨论,他扩展了设计背后的思考:

In the following comp.programming.threads discussion, he expands on the thinking behind the design:


Patrick Doyle wrote: 
> In article , Tom Payne   wrote: 
> >Kaz Kylheku  wrote: 
> >: It is so because implementations can sometimes not avoid inserting 
> >: these spurious wakeups; it might be costly to prevent them. 

> >But why?  Why is this so difficult?  For example, are we talking about 
> >situations where a wait times out just as a signal arrives? 

> You know, I wonder if the designers of pthreads used logic like this: 
> users of condition variables have to check the condition on exit anyway, 
> so we will not be placing any additional burden on them if we allow 
> spurious wakeups; and since it is conceivable that allowing spurious 
> wakeups could make an implementation faster, it can only help if we 
> allow them. 

> They may not have had any particular implementation in mind. 

You're actually not far off at all, except you didn't push it far enough. 

The intent was to force correct/robust code by requiring predicate loops. This was 
driven by the provably correct academic contingent among the "core threadies" in 
the working group, though I don't think anyone really disagreed with the intent 
once they understood what it meant. 

We followed that intent with several levels of justification. The first was that 
"religiously" using a loop protects the application against its own imperfect 
coding practices. The second was that it wasn't difficult to abstractly imagine 
machines and implementation code that could exploit this requirement to improve 
the performance of average condition wait operations through optimizing the 
synchronization mechanisms. 
/------------------[ David.Buten...@compaq.com ]------------------\ 
| Compaq Computer Corporation              POSIX Thread Architect | 
|     My book: http://www.awl.com/cseng/titles/0-201-63392-2/     | 
\-----[ http://home.earthlink.net/~anneart/family/dave.html ]-----/ 

这篇关于为什么调用pthread_cond_wait有虚假唤醒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆