[cpp-threads] Alternatives to SC

Doug Lea dl at cs.oswego.edu
Fri Jan 12 14:34:45 GMT 2007


Herb Sutter wrote:

 >
 > 2. What are we gaining?

If nothing else, good standardization pragmatics:
The standard can be adopted with the belief that efficient compliant
implementations surely exist across the various platforms C++
might be expected to run on.

In which case, among other things, people can stop
arguing about whether you might need to implement reads using CAS
or LL/SC. (The performance impact of requiring CAS is easy to
approximate. Programs on average have 80:20 read to write ratios.
A CAS or LL/SC is at best 20 times slower than a load (and
rapidly gets worse with #CPUs). So you'd expect at least a factor
of 16 slowdown in any program solely using atomics.)


> 
> 1. What are we giving up? 

Mainly, we give up on a one-phrase characterization of strong atomics (i.e.,
the operations corresponding to Java volatile) -- "Sequential consistency".
And replace it with two phrases: "Cache coherence" and "Causal consistency",
both of which programmers mixing atomics with non-atomics will need to
understand anyway.

Again: programs solely using locks remain sequentially consistent, and
programs solely using ordinary variables remain racy, and programs
mixing either of these with atomics are defined as non-racy only
under certain conditions.


And the choice relies entirely on whether to guarantee a
deterministic outcome for independent reads of independent writes.

It would be great to hear people's opinions about this.
For easy reference, here's the canonical example. (In fact,
basically the only example. I haven't seen any that don't
reduce to this one in some obvious way).

Initial: strong-atomic/volatile x = y = 0
T1      T2         T3      T4
x = 1   y = 1
                   r1 = x   r3 = y
                   r2 = y   r4 = x
Query: Is it possible for r1 == 1 && r2 == 0 && r3 == 1 && r4 == 0

The only reactions to this I've ever gotten from Java programmers are
1. "I don't know and don't care. I stay away from volatiles"
2. "Of course it is allowed. There's no happens-before relation there"
3. "I refuse to answer trick questions about corner cases in JMM spec."

-Doug









More information about the cpp-threads mailing list