[concurrency-interest] AtomicReferenceweakCompareAndSet "Mayfailspuriously"?

Doug Lea dl at cs.oswego.edu
Mon May 29 07:35:12 EDT 2006


Brian Goetz wrote:
>> 
>   - The specification of CAS currently requires a full fence.
>   - Some processors support CAS operations that do not require full 
> fences.
>   - On these processors, CASing without a fence is far cheaper, and 
> scales much better.
>   - Performance counters are important in concurrent applications, and 
> are, by definition, performance-critical.
>   - We're teaching everyone that AtomicInteger.getAndIncrement is the 
> right way to implement a performance counter.
>   - So, shouldn't getAndIncrement be able to use the fence-less CAS on 
> processors that support it?
> 

I don't think so. To get the desired effect here, it would be
called "weakMaybeIncrement" or somesuch, which would
expand out into just

void weakMaybeIncrement(AtomicInteger ai) {
   int i = ai.get();
   ai.weakCompareAndSet(i, i+1);
}

I think that anyone sensitive to these issues will be able
to write these two lines themselves.


-Doug


More information about the Concurrency-interest mailing list