[cpp-threads] Failed compare-and-swap

Jeremy Manson jmanson at cs.umd.edu
Thu Aug 2 05:05:50 BST 2007


Bill Pugh wrote:
> I cast my vote for saying that a failed compare and swap or failed  
> trylock should have no synchronization semantics.
> 
> The only idioms I know of that would depend on failed atomic  
> operations having synchronization semantics are so problematical that  
> we should do everything we can to discourage people from trying to  
> use them.
> 
> By giving fail operations semantics, you also rule out possible  
> implementation strategies that might be advantageous in some current  
> or future platform.
> 
> Bill Pugh


I don't think I agree with Bill on this (not that I pipe up very often 
on this list).  It seems to me that you could have a perfectly 
reasonable multithreaded non-blocking singleton idiom that depended on 
this.  Basically, if you were going to have exactly one of an object, 
you could do an atomic load to see if it were set; if it weren't, you 
could optimistically construct it and do a CAS to do the assignment to 
see if it were updated in the mean time (deleting the object if the CAS 
fails).

It seems to me that the ordering constraint for failed CAS could very 
well be parametric.

I don't see any justification for trylock, though.

					Jeremy



More information about the cpp-threads mailing list