[concurrency-interest] Stricter read ordering

Aleksey Shipilev aleksey.shipilev at oracle.com
Wed Apr 23 08:42:05 EDT 2014


On 04/16/2014 03:25 PM, Tobias Lindaaker wrote:
> Is there any way I could introduce a fence that guarantees that the data
> reads will not be moved to after the read of this.next?

I think you are reinventing sequence locks. Prime Java example is
StampedLock which deals with ordering reads with Unsafe.loadFence(), see
StampedLock.validate().

So, in your example, it amounts to:

       public DataCarrier read() {
         // I allow multiple readers, so this method is not ynchronized
         int x, y;
         long version;
         do {
           version = this.written;
           x = this.x;
           y = this.y;
           Unsafe.loadFence();
         } while ( version != this.next );
         return new DataCarrier( x, y );
       }
     }

-Aleksey.



More information about the Concurrency-interest mailing list