Re: JavaMemoryModel: Ordering of volatile and monitor actions

From: Joseph Bowbeer (jozart@csi.com)
Date: Thu Jun 28 2001 - 03:52:08 EDT


PS - Here's a slightly simpler version (showing that reordering monitor exit
and volatile read can lead to deadlock).

Initially:

    volatile boolean consumed = true;

Thread 1:

    while (true) {
        synchronized(this) {
            work();
            consumed = false;
        }
        while (!consumed) { /* wait */ }
    }

Thread 2:

    while (true) {
        synchronized(this) {
            if (!consumed) {
                consumeWork();
                consumed = true;
            }
        }
    }

As before, thread 2 is using the volatile flag 'consumed' to signal thread 1
to stop waiting and do some more work. If the read of 'consumed' in thread 1
is moved into the synchronized block, then the program deadlocks.

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:32 EDT