Referring to (slightly outdated) paper by Hans Boy in the "Atomic Operations" section. It mentions that the memory model (proposed at the time) will not prevent the optimization compiler from combining a sequence of loads or storages from the same variable from combining into a single load. His example is as follows (updated, hopefully correct syntax):
Considering
atomic<int> v;
The code
while( v.load( memory_order_acquire ) ) { ... }
It can be optimized for:
int a = v.load(memory_order_acquire); while(a) { ... }
Obviously, that would be bad, as he claims. Now my question is, since the document is a bit old, does the current C ++ 0x memory model prevent this type of optimization or is it still technically resolved?
My reading of the standard seems to be biased, but using the semantics of “acquire” makes it less clear. For example, if it is "seq_cst", the model seems to guarantee that loading should participate in the general order when accessing and loading the value only once, thus, it seems to disrupt the order (since it breaks the sequence, it occurs before the relation).
By purchasing, I interpret 29.3.2 to mean that this optimization cannot happen, since any “release” operation must be observed during the “acquire” operation. Running only one instance seems invalid.
So my question is, will the current model (in the pending standard) prohibit this type of optimization? And if so, which part specifically prohibits this? If not, is the volatile atom using the problem?
And for the bonus, if the download operation has a “relaxed” ordering, is optimization allowed?