Volatile and code reordering

R

REH

I believe the consensus of this group is that volatile does not
protect against code reordering. But I am not convinced. The standard
says that "accesses to volatile objects are evaluated strictly
according to the rules of the abstract machine." A later note states
"An implementation might define a one-to-one correspondence between
abstract and actual semantics: at every sequence point, the values of
the actual objects would agree with those specified by the abstract
semantics. The keyword volatile would then be redundant."

I would take the above to mean that it does guarantee order (a least
relative to other volatile sequence points). Assuming that the
reordering of statements is an optimization, and volatile inhibits
optimization, it follows that volatile would guarantee order. I am
missing something, or am I misinterpreting the above quotes?

Thanks,
REH
 
R

REH

     The accesses to volatile objects cannot be reordered with
respect to each other or with respect to other observable side-
effects (except between bracketing sequence points).  But their
order w.r.t. other parts of the code that do not produce side-
effects remains uncertain -- you can think of this as meaning
that the volatile accesses can't move, but the non-volatile
pieces still can.

        volatile int x;
        int y;
        x = 1;
        y = 2;
        x = 3;
        y = 4;
        x = 5;

In this fragment all three accesses to `x' must occur, and must
occur in order.  But the accesses to `y' are a different matter:
the compiler is free to rearrange them w.r.t. each other and w.r.t.
the accesses to `x'.  Indeed, the compiler may choose to discard
the first `y' access, or even both of them if it notices that `y'
is not used after its second assignment.

Thank you, that is what I thought. Just wanted to make sure!

Regards,
REH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top