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

Boehm, Hans hans.boehm at hp.com
Fri Jan 20 20:27:02 EST 2012


> From: Raph Frank [mailto:raphfrk at gmail.com]
> 
> Thanks for the info.
> 
> On Fri, Jan 20, 2012 at 11:10 AM, Ruslan Cheremin <cheremin at gmail.com>
> wrote:
> > long seq = sequence.get();
> > ...some reading...
> > if( !sequence.CAS(seq, seq) ){
> >   //...approach failed -> retry
> > }
> >
> > So, from my point of view, if CAS failed -- we shouldn't actually
> care
> > about it's ordering semantic (although it was interesting to know --
> > thanks, David -- what ordering does not depend on success/fail). If
> > CAS succeeded -- it does required guarantee anyway. Am I wrong
> > somewhere here?
> 
> Ahh right, that is better than
> 
> if (sequence.getAndAdd(0) != seq) {
>   <retry>
> }
> 
> Anyway, thanks all for the info.

Thanks for the corrections and clarifications.  It does look like we should essentially view CAS as always performing a volatile store, possibly of the original value.

It seems to me that the trade-off between CAS and getAndAdd here is highly implementation dependent.  Clearly if getAndAdd(0) is implemented in terms of CAS(x,x), CAS is faster.  I suspect either could in theory be optimized to a plain old MOV + compiler constraints on x86.

Hans

 




More information about the Concurrency-interest mailing list