Volatile is Necessary

Barnacle Wes wes at obie.UUCP
Thu Mar 31 04:28:06 AEST 1988


In article <8107 at sol.ARPA>, crowl at cs.rochester.edu (Lawrence Crowl) writes:
> The `volatile' type qualifier of ANSI C has recently been criticized as a
> frill.  While this assertion is arguable for uniprocessors, it is not true for
> shared-memory multiprocessors.  Processes on such machines communicate via
> updates to a shared variables.  These updates may occur at arbitrary times
> relative to one of the observing processes.  If there is no mechanism to
> indicate when a variable referenced by one process may be modified by another
> process, the compiler must assume that all variables may be modified at any
> time.

The `volatile' qualifier is also a must for such things as I/O
processors that do not produce interrupts.  Typically with such a
device, you will poll the status register at a predetermined interval,
and when the operation is complete, you can go on to the next
operation on that device.

These types of devices are used quite a bit in imbedded systems where
the speed of the device is not critical, and the interrupt(s) are
already heavily used for speed-critical devices/operations.  You
certainly don't want the compiler to optimize away your check on the
status register!

-- 
    /\              -  "Against Stupidity,  -    {backbones}!
   /\/\  .    /\    -  The Gods Themselves  -  utah-cs!utah-gr!
  /    \/ \/\/  \   -   Contend in Vain."   -  uplherc!sp7040!
 / U i n T e c h \  -       Schiller        -     obie!wes



More information about the Comp.lang.c mailing list