Re: wait and spurious wakeups
Patricia Shanahan wrote:
Lew wrote:
All a return from wait() guarantees is that you have the lock, not
that the condition has changed.
I think the true spurious wake-up question is whether wait can return
without timeout or lock acquisition. In some contexts, including the one
described at the start of this thread, if wait has acquired the lock
then the resource is available.
The object's monitor is re-acquired before the caller regains
control. "Regains control" covers plain returns, returns after a
time-out, and the throwing of exceptions from inside the wait call.
In practice, I always use the "while" loop because it is definitely more
robust. Using "if" breaks if the program changes in any of several ways.
My real question is whether there can be a truly spurious wake-up
without a notifyAll or timeout. Is it real, or is it an urban legend?
The Wikipedia article quotes Butenhof as expressing some doubt
about the reality of spurious wakeups (and he's certainly in a good
position to know). It might turn out to be legend and FUD, after
all. However, the folks who implement the synchronization primitives
are by now convinced that they have a license to wake up spuriously
if they ever find a good reason to do so, so today's FUD might become
tomorrows fad ... It's sort of like those "Reserved for future
expansion; must be zero" things you find all over the place: You
might get away with stuffing non-zeroes into them, and you might
even get away with it forever; anticipated expansions don't always
come to pass. But sometimes they do, and then ...
comp.programming.threads is probably the right forum for pursuing
this further.
--
Eric.Sosman@sun.com