Spurious wakeup

In the multithreaded programming, it is very common to create a structure where a thread waits for signals from another one. An example can be a program with a graphic interface: the graphics thread, used to manage user inputs, can be blocked while another thread downloads information: what if the graphics thread is waked up without a reason?

Condition Variables

One of the base functionality in the multithread world is the condition variables:

public class MyFantasticObject {
    GenericObject myObject = new GenericObject();
    boolean condition = false;

    public void methodOne() {
        if(!condition) {
            myObject.wait();
        }

       // Some operations...
    }

    public void methodTwo() {
        condition = true;
        myObject.notify();
    }
}

Let's say that a thread A calls methodOne() to perform some operations: during its execution, if condition is not verified, the thread A is put on inactive state using the wait() method.

Another thread B can start its work and, after a while, call methodTwo() in order to notify to thread A to continue its work.

It seems easy, doesn't it?

Time to wake up

Here comes the bad part: the thread A can be waked up even if no one has called the notify() method.

It is a Spurious Wakeup.

It is a known issue and it is mentioned in the Posix documentation, so we need to deal with it (and luckily it isn't so difficult).
The solution is to change the implementation of the methodOne() in order to insert the wait() method inside a while loop rather than an if-statement: in this way we can check the value of the condition variable in case a spurious wakeup appears.

Here is the changed version of the code:

public class MyFantasticObject {
    GenericObject myObject = new GenericObject();
    boolean condition = false;

    public void methodOne() {
        while(!condition) {
            myObject.wait();
        }

        // Some operations...
    }

    public void methodTwo() {
        condition = true;
        myObject.notify();
    }
}

Now if the spurious wakeup appears, the thread won't continue the execution of the code until the condition value changes.

Conclusion

After starting to study Spurious Wakeups, I've tried to find out why they happen and why no one has tried to fix it.

Let's be honest: a non-deterministic wait() method is not so good.

Seems that finding out the reason behind them is harder than expected, but I've found a good article written by Vladimir Prus in the [Wikipedia page](https://en.wikipedia.org/wiki/Spurious_wakeup" Wikipedia page"): it's hard to fix.
In order to fix them, we need a big change in certain architectures and it means that fixing one bug, we risk to generate more of them.
This takes more time and effort than teaching Spurious Wakeup to every future developer and the solution to put it in a while loop is easy enough to leave them where they are.

The article and the written code are easy to read and to understand, but they lack details: I suggest you read the suggested article by Vladimir Purs and other interesting around the web, like this, this and this..

Happy spurious wakeup,
M.