[concurrency-interest] Stricter read ordering

Tobias Lindaaker thobes at gmail.com
Wed Apr 23 09:05:10 EDT 2014


Yes, I had a look at StampedLock and Unsafe.loadFence(), and it seems to do exactly what I want, and if I was fortunate enough to be able to move to Java 8 I would use it. Unfortunately we are still stuck on Java 7. We even have customers who are still strongly requesting Java 6 compatibility.

-tobias

On 23 Apr 2014, at 14:42 , Aleksey Shipilev <aleksey.shipilev at oracle.com> wrote:

> 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