为什么pthread_cond_wait有虚假的唤醒?

引用手册页:

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可以返回,即使你没有信号。乍一看,至少,这似乎很可怕。它将像一个函数,它随机返回错误的值或在实际达到正确的return语句之前随机返回。它似乎是一个大bug。但事实上,他们选择在手册页中记录这个而不是修复它似乎表明有一个合法的理由,为什么pthread_cond_wait结束了虚假的唤醒。大概,有一些内在的它如何工作,使得这是不能帮助。问题是什么。

为什么pthread_cond_wait返回伪造?为什么不能保证它只有在被正确发出信号时才醒来?任何人都可以解释其虚假行为的原因吗?

以下解释由David R.Butenhof在“Programming with POSIX Threads”(第80页)中给出:

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

在接下来的comp.programming.threads discussion年,他扩展了设计思路:

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 ]-----/ 

http://stackoverflow.com/questions/8594591/why-does-pthread-cond-wait-have-spurious-wakeups

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:为什么pthread_cond_wait有虚假的唤醒?