[concurrency-interest] Suggestion: .hardGet() for atomic variables

Ruslan Cheremin cheremin at gmail.com
Thu Jan 19 16:25:34 EST 2012


It depends on what a.increment (getAndIncrement, actually) returns. If
it is the same value, as you got by getAndAdd(0) in second thread --
yes, it HB(getAndAdd(0), a.getAndIncrement()).

In your example of optimistic lock, if you would chang second .get()
in optimistic read to .getAndAdd(0) -- it seems for me that your lock
will be ok.

2012/1/20 Raph Frank <raphfrk at gmail.com>:
> On Thu, Jan 19, 2012 at 9:04 PM, Ruslan Cheremin <cheremin at gmail.com> wrote:
>> I do not know, does current
>> CPUs actually optimize this writeback.
>
> Well, I am just wondering what the model says.
>
> does .getAndAdd(0) count as a write for the purposes of creating a
> happens-before chain?
>
> So, if the sync order was
>
> AtomicInteger a = new AtomicInteger(0);
> int nonVolatile = 0;
>
> Thread 1         <----------->         Thread 2
>
>                                       int b = nonVolatile;
>                                       int aTemp = a.addAndSet(0);
>
> a.increment();
> nonVolatile = 1;
>
>                                       System.out.println("Atemp = " + aTemp);
>                                       System.out.println("b = " + b);
>
> System.out.println("a is " + a.get());
>
>
> Is a happens-before relationship established between b = nonVolatile
> and nonVolatile = 1?



More information about the Concurrency-interest mailing list