[cpp-threads] C++ memory model

Boehm, Hans hans.boehm at hp.com
Wed Sep 23 00:49:57 BST 2009


> From:  Peter Dimov
> 
> Boehm, Hans wrote:
> 
> > I'd like to just delete the second senetnce:
> >
> > "Implementations shall not move an atomic operation out of an 
> > unbounded loop."
> 
> I'd like to cast my vote in favor of the sentence. It does 
> indeed carry little formal value, but in practice, it can 
> (easily) settle a hypothetical debate between a compiler user 
> and a compiler writer about whether such an optimization is allowed. 
> 
What about

atomic<int> x;

Thread 1:
<reads x now and then, but doesn't write it>

Thread 2:
for (;;) x = 17;

Assume the compiler can deduce that there are no other threads writing to x.

Does the above sentence prevent the compiler from transforming the thread 2 code to:

x = 17; for (;;);      ?

Strictly, I think the answer is "yes".  But I don't think there's any way the program can tell.  What does a prohibition on program transformations mean in light of the "as if" rule?

If this is allowed under the "as if" rule, am I allowed to justify a transfomation to

for (;;){} x = 17;

by arguing that if you wait for 20 years and nothing happened, the behavior is as if this thread just hadn't made progress for 20 years, which is allowable behavior?

Alternatively, is it OK to implement this on a machine with a store buffer that gets flushed only on overflow, and since there are no stores except to x, the buffer never gets flushed?  (Or I might have an interpreter that exhibits this behavior.)

I'm just not sure what such a statement means.

Hans



More information about the cpp-threads mailing list