This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Problem with reordering of memory accesses


Michael Schwingen <michaels@stochastik.rwth-aachen.de> writes:

> How do I ensure that the writes occur in the specified order?  
> 
> Declaring x2 and x4 "volatile" helps in this test case, but not in my real
> (more complex) code. Declaring the pointer volatile seems to work, but I
> would like to use some construct that is *guaranteed* to work even with
> newer compiler versions. Is the solution with the volatile pointer safe? If
> not, what alternatives can I use?	

'volatile' should be the solution you need.  If you declare everything
whose ordering you care about 'volatile', then those variables should
be changed in the exact order you specify.  If not, it's a bug.

In the particular case you had, declaring the pointer 'volatile' would
not reliably work.  For instance, if you wrote

a->b = 3;
a->c = 4;

then what GCC can do is read 'a' twice, and then write to c and then
write to b.  I don't think it would ever do this, but it could.

-- 
- Geoffrey Keating <geoffk@cygnus.com>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]