volatile (in comp.lang.c)
Stephen Crawley
scc at cl.cam.ac.uk
Fri May 20 11:45:06 AEST 1988
In article <166 at iaoobelix.UUCP> woerz at iaoobelix.UUCP (Dieter Woerz) writes:
>In article <1011 at ima.ISC.COM> johnl at ima.UUCP (John R. Levine) writes:
>> .... I have never seen any suggestion that a program containing
>>"volatile" would be portable except perhaps to other processors which happen
>>to have similar memory, I/O, and hardware architectures.
>> ...
>
>I don't see, what "volatile" has todo with the hardware architecture.
>In my understanding, "volatile" simply says that the compiler has to
>access the variable in the memory every time the variable is access-
>ed. There seems to be nothing dependent on the hardware, exept that
>you can't get access to I/O ports on architectures, which don't have
>memory mapped I/O. So "volatile" has nothing todo with portability,
>because every compiler can be made to access a variable in memory
>every time it is read or writtten.
>
The "volatile" construct is also necessary to write portable multi-process
applications that use shared memory. Consider two processes A and B with
a shared variable v, and the following code
int x = v /* A1 */ | v = 2 /* B1 */
int y = v /* A2 */ |
Assume that v starts with the value 1, and that the statements are actually
executed in the order A1,B1,A2.
If the C compiler generates a memory fetch for each access to v, A's
variables x and y will contain 1 and 2 respectively. If the compiler
does not generate a memory fetch for each access, x and y will both
contain 1. To get behaviour that is the same for all compilers, the
variable v must be declared volatile.
[It might be argued that if processes A and B ought were using some
synchronisation mechanism (e.g. a Unix semaphore call after A1) and
that an optimizing compiler would notice this and realise that
statement A2 needed to have a fetch.
I claim that such an argument is invalid. In some tightly coupled
multiprocessor systems, shared memory cells are the basis for ALL
synchronisation. If the compiler won't let me declare volatile
variables, I can't implement synchronisation primitives. Even in
a uniprocessor UNIX system, there are situations where it is better
to use a shared variable for synchronising 2 processes rather than
frittering away a couple of milli-seconds on semaphore system calls]
-- Steve
More information about the Comp.lang.c
mailing list