[concurrency-interest] AtomicReferenceweakCompareAndSet "Mayfailspuriously"?

Doug Lea dl at cs.oswego.edu
Mon May 29 10:52:11 EDT 2006


Jeremy Manson wrote:
> 
> 
> More worrisome is the fact that there isn't really any meaningful use 
> case for a happens-before-free weakCompareAndSet on an AtomicReference 
> (AFAICT).  

No common ones, but as we've seen, use cases for weakCAS
aren't all that common to begin with. One use case is in
help-out steps of wait-free algorithms, of the form:

retry:
   read-volatile n = head.get();
   read-volatile next = n.next.get();
   if (inconsistent(next)) {
        n.next.weakCompareAndSet(next, fixednext);
        goto retry;
   }
   ...

Because of full retry, and the likelihood that a retry by itself
will suffice (i.e., probably some other thread has already fixed
link), you might as well use the cheapest form. (Or maybe not,
depending on the relative likelihoods. It's an empirical issue though.)

Among others, Michael-Scott queues have steps of this basic form,
although the j.u.c ConcurrentLinkedQueue version use strong CAS in this
sort of case. It could be changed a bit to use weak though.

Aside: It might be nice if there were javadoc tags saying
"you probably {do, don't} want to use this method".  For the
sake of completeness, many APIs have methods that are only
rarely needed. But indispensible when they are needed.


-Doug



More information about the Concurrency-interest mailing list