[concurrency-interest] Fwd: Should I avoid compareAndSet with value-based classes?

Doug Lea dl at cs.oswego.edu
Fri Jul 7 08:20:19 EDT 2017


On 07/07/2017 05:20 AM, Andrew Haley wrote:

> In practice it doesn't matter, because you don't need to CAS a
> reference to an Instant: all you need to do is to wrap all of the
> updates to that reference in synchronized blocks.  Readers don't need
> anything more than for that reference to be volatile.  The
> immutability of Instant and the sequential consistency of volatile
> provide all the guarantees that anyone needs or are possible.
> 
> I doubt that a synchronized block is going to perform much more badly
> than spinning in a CAS loop, especially in the presence of high
> contention.  In the many readers/few writers situation that Michael
> describes this is the optimal solution.
> 

Although amenable to the VarHandle tweak of using getAcquire
(vs implicit getVolatile) for reads. This tiny loosening of sequential
consistency might improve performance on ARM and POWER.
It's the Volatile/locked-write, acquiring-read pattern mentioned in
  http://gee.cs.oswego.edu/dl/html/j9mm.html#mixedvolatilesec

-Doug


More information about the Concurrency-interest mailing list