[cpp-threads] Failed compare-and-swap

Peter Dimov pdimov at mmltd.net
Wed Aug 1 18:35:02 BST 2007


Herb Sutter wrote:

> Why do you think a failed CAS can be used to read the old value if
> you have a boolean success/fail result?

It can if it returns both a success/fail result and an old value. It is 
possible for such a CAS to fail even if the old value is equal to the 
comparand (ABA, context switches between LL and SC).

>  - CAS returns the old value, in which case a failed CAS should at
> least by default have acquire semantics because it's a read.

Mixing seq_cst and acquire does not necessarily produce a 
sequentially-consistent program (unless this is guaranteed by the MM, it 
wasn't last I checked). Think of the canonical

// thread 1
x = 1;
r1 = CAS( &y, 5 );

// thread 2
y = 1;
r2 = CAS( &x, 5 );

Unless CAS is equivalent to load/seq_cst in the failure case, or 
load/acquire+store/seq_cst is SC-blessed by the memory model (questionable 
because of IRIW), (0,0) is a possible outcome if failed CASes only have 
acquire semantics. 




More information about the cpp-threads mailing list