[concurrency-interest] increment()/decrement() methodson atomicInteger ?

David Holmes dholmes at dltech.com.au
Mon May 30 18:19:00 EDT 2005


Brian Goetz wrote:
> This does not save the CAS retry cycle.  Increment looks like this:
>
> while (true) {
>    int i = get();
>    if (cas(i, i+1))
>      return i+1;
> }
>
> To implement a non-value-returning increment, you'd still have to do this:
>
> while (true) {
>    int i = get();
>    if (cas(i, i+1))
>      break;
> }
>
> So there's no performance advantage to having a non-value-returning
> increment operation.

I think Brian and Doug have missed Yechiel's point. If I understand
correctly Yechiel is presuming that increment() et al use a CAS-loop only
because they want to return the old value. Therefore if you don't need the
return value you could implement those methods without using CAS at all.
This is true in principle. On X86 there are explicit instructions that can
be used to do atomic increments and decrements. However - and Doug is the
expert on this - I recall being told that there is no significant
performance gain in using such instructions over using a CAS loop. So all
the atomic ops were written using the CAS-loop, which not only simplifies
the Java code, it also makes life easier for the JIT/VM as it only has to
intrinsify one atomic instruction.

Cheers,
David Holmes



More information about the Concurrency-interest mailing list