[cpp-threads] Failed compare-and-swap

Herb Sutter hsutter at microsoft.com
Wed Aug 1 18:17:55 BST 2007


I forgot to address one detail of what Hans wrote yesterday:

I wrote:
> > Summarizing:
> >
> > The point well illustrated by these examples is that if CAS
> > returns the old value (rather than a bool) it can be used to
> > perform an atomic load. If we allow that, then it should have
> > the same default acquire semantics as a load (unless
> > overridden in the weak/relaxed atomics of course).
> >
> > Lawrence and everyone, what do you think? Now that I see
> > this, it seems obvious in retrospect, and this question must
> > have come up before with CAS semantics. What do other systems do?

Hans wrote:
> I completely agree with this.  If a CAS can be used to read the old
> value (which it can, arguably if it only has a boolean result),

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

> and it
> has at least acquire semantics, then it should still have acquire
> semantics even if it fails and the write doesn't take place.
> The read still succeeds.

It seems like there are two legitimate choices:

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

  - CAS returns only a success/fail boolean, in which case a failed CAS need not have any ordering semantics.

Does that make sense? It seems we can happily choose either, but the choice affects the return type.

Herb




More information about the cpp-threads mailing list