[concurrency-interest] jdk9 VarHandle and Fence methods

Doug Lea dl at cs.oswego.edu
Fri Aug 21 18:58:38 EDT 2015


On 08/21/2015 04:47 PM, Peter Levart wrote:
> Hi Doug,
>
> On 08/21/2015 03:44 PM, Doug Lea wrote:
>> /**
>>      * Atomically sets the value to the given updated value with the
>>      * memory semantics of setRelease if the current value {@code ==}
>>      * the expected value, as accessed with the memory samantics of
>>      * getRelease.
>              ^^^^
> Should that be getRelaxed or more probably getAcquire ?

Thanks; fixed. It should say getRelaxed.

>
>> *
>>      * @param expected the expected value
>>      * @param val the new value
>>      * @return the current value, which will be the same as {@code val} if
>>      * successful.
>>      */
>>     T compareAndExchangeRelease(Object owner, T expected, T val);
>
> Suppose the value is currently 1 and two threads do the following concurrently:
>
> Thread1:
>
> v1 = compareAndExchangeRelease(owner, 1, 2);
>
> Thread2:
>
> v2 = compareAndExchangeRelease(owner, 1, 3);
>
>
>
> Can the outcome be: v1 == 2, v2 == 3 ?
>
> If it is getAcquire then probably not, but if it is getRelaxed then It might be
> or not?

No; the CAS "confirms" the value. As Vitaly noted, the modes are
there for finer-grained control of what happens before/after the CAS.
(Note: on X86 and Sparc, all flavors of CAS map to the same instruction,
but they may all differ on ARM and POWER.)

-Doug



More information about the Concurrency-interest mailing list