* Optimizer reordering question?
@ 2004-09-22 8:47 Trevor Lobban
0 siblings, 0 replies; only message in thread
From: Trevor Lobban @ 2004-09-22 8:47 UTC (permalink / raw)
To: gcc-help
Hi,
I have a question on how I can write a piece of code such that I can still
get the best performance but can also guarantee it's integrity. Below is a
simplified example of what I am try to do.
I have some data which resides in shared memory, and in order to protect
against multiple updates I use a lock, however I also need to protect
against a process dying half-way through an update. So I use code similar to
the following:
Lock(pshared);
if (pshared->UpdateInProgress)
doRecovery(pshared);
pshared->UpdatedInProgress=TRUE;
pshared->value1 = localValue1; /* The critical updates */
pshared->value2 = localValue2;
pshared->value3 = localValue3;
pshared->UpdateInProgress=FALSE;
UnLock(pshared);
If when a process gets the lock, he sees that UpdateInProgress is TRUE it
knows it has some recovery to do. However my problem is how can I protect
against the optomizer re-ordering the code. I guess I need to define the
UpdateInProgress flag as volatile, but do I also need to declare the value1,
value2 and value3 is pshared as volatile too in order to guarantee that they
don't get re-ordered with respect to the UpdateInProgress flag?
Appologies for the rather long-winded explanation, but I figured it was the
best way of explaining what I am try to achieve. Any guidance is much
appreciated.
Thanks,
Trevor
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2004-09-22 8:47 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-22 8:47 Optimizer reordering question? Trevor Lobban
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).