[concurrency-interest] Stricter read ordering

Vitaly Davidovich vitalyd at gmail.com
Wed Apr 23 09:05:03 EDT 2014


Right.  Otherwise, the loads of x and/or y can move after the load of next
and you may read "ahead" (I.e. read of next catches the old unchanged value
but x and y catch the newly written ones, leading to loop exiting but
having newer x and y that don't match next).

Sent from my phone
On Apr 23, 2014 8:47 AM, "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.
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140423/e27e7a29/attachment.html>


More information about the Concurrency-interest mailing list