tags:

views:

849

answers:

4

Seeing various locking related question and (almost) always finding the 'loop because of spurious wakeups' terms1 I wonder, has anyone experienced such kind of a wakeup (assuming a decent hardware/software environment for example)?

I know the term 'spurious' means no apparent reason but what can be the reasons for such kind of an event?

(1 Note: I'm not questioning the looping practice.)

Edit: A helper question (for those who like code samples):

If I have the following program, and I run it:

public class Spurious {
    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition cond = lock.newCondition();
        lock.lock();
        try {
            try {
                cond.await();
                System.out.println("Spurious wakeup!");
            } catch (InterruptedException ex) {
                System.out.println("Just a regular interrupt.");
            }
        } finally {
            lock.unlock();
        }
    }
}

What can I do to wake this await up spuriously without waiting forever for a random event?

A: 

Cameron Purdy wrote a blog post a while back about being hit by the spurious wakeup problem. So yes, it happens

I'm guessing it's in the spec (as a possibility) because of limitations of some of the platforms which Java gets deployed on? although I may be wrong!

oxbow_lakes
http://www.jroller.com/cpurdy/entry/java_supplies_apologies_to_weird
kd304
I read the post and gave me an idea about having unit tests for testing one application's conformance to the looping-wait paradigm by waking it up randomly/deterministically. Or is it already available somewhere?
kd304
It's another question on SO: "Is there a *strict* VM that can be used for testing?". I'd love to see one with strict thread-local memory - I don't think they exist yet
oxbow_lakes
Thank you for the blog reference. +1, because you pointed out the other question.
kd304
A: 

This blog has a good discussion on it. Apparently it is due to some of the hardware designs that Java runs on.

Kathy Van Stone
Thank you. The post describes the specification and effects for such event but only a few words about the causes as I read it.
kd304
It points to a reference about the causes (in a book), but I suspect that any explanation beyond "some hardware implementations find it simpler to allow this" will be extremely low level and subtle. In particular it requires reading secton 11.4.3.6.1 of the Posix standard (which is apparently not available for free).
Kathy Van Stone
Thank you. +1 for the book reference.
kd304
+6  A: 

The Wikipedia article on spurious wakeups has this tidbit:

The pthread_cond_wait() function in Linux is implemented using the futex system call. Each blocking system call on Linux returns abruptly with EINTR when the process receives a signal. ... pthread_cond_wait() can't restart the waiting because it may miss a real wakeup in the little time it was outside the futex system call. This race condition can only be avoided by the caller checking for an invariant. A POSIX signal will therefore generate a spurious wakeup.

Summary: If a Linux process is signaled its waiting threads will each enjoy a nice, hot spurious wakeup.

I buy it. That's an easier pill to swallow than the typically vague "it's for performance" reason often given.

John Kugelman
Good findings. Thank you.
kd304
Thank you. You definitely gave a correct answer to all three questions. Will do further research/trials based on this.
kd304
Better explanation here: http://stackoverflow.com/questions/1461913/does-c-monitor-wait-suffer-from-spurious-wakeups/1461956#1461956
Gili
A: 

I have a production system that exhibits this behaviour. A thread waits on a signal that there is a message in the queue. In busy periods, up to 20% of the wakeups are spurious (ie when it wakes there is nothing in the queue). This thread is the only consumer of the messages. It runs on a Linux SLES-10 8-processor box and is built with GCC 4.1.2. The messages come from an external source and are processed asynchronously because there are problems if my system does not read them fast enough.

Mr.Dirty.Birdy