This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
question on semantics
- From: Chris Friesen <cfriesen at nortelnetworks dot com>
- To: gcc at gcc dot gnu dot org
- Date: Wed, 04 May 2005 11:59:49 -0600
- Subject: question on semantics
- Newsgroups: gnu.gcc
I'm not sure who I should address this to...I hope this is correct.
If I share memory between two processes, and protect access to the
memory using standard locking (fcntl(), for instance), do I need to
specify that the memory is volatile? Or is the fact that I'm using
fcntl() enough to force the compiler to not optimise away memory accesses?
As an example, consider the following code, with <lock>/<unlock>
replaced with the appropriate fcntl() code:
int *b;
int test()
{
b=<address of shared memory>;
while(1) {
<lock>
if (*b) {
break;
}
<unlock>
}
<unlock>
return *b;
}
Without the locks, the compiler is free to only load *b once (and in
fact gcc does so). Is the addition of the locks sufficient to force *b
to be re-read each time, or do I need to declare it as
volatile int *b;
Thanks,
Chris