Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 15 Jan 2000 20:50:22 +0100 (CET)
From:      Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de>
To:        freebsd-questions@FreeBSD.ORG
Subject:   Re: Volatile variables
Message-ID:  <200001151950.UAA86770@dorifer.heim3.tu-clausthal.de>
In-Reply-To: <85kinv$2ffa$1@atlantis.rz.tu-clausthal.de>

next in thread | previous in thread | raw e-mail | index | archive | help
Mikhail Evstiounin <evstiounin@adelphia.net> wrote in list.freebsd-questions:
 > From: Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de>
 >>You _do_ need "volatile" in that case.  It is necessary for
 >>all variables whose contents can change asynchronously, i.e.
 >>outside of the normal program flow.  For example, this is true
 >>for variables which are mapped to hardware registers, and for
 >>variables which are located in a shared memory reagion (which
 >>is shared with other processes), and for variables which are
 > 
 > that is - in ways not specified by the language.
 > 
 >>accessed from within signal handlers.
 > 
 > This is, in my mind, slightly different.

No, it's _exactly_ the same, as far as the compiler is
concerned.  The meaning of "volatile" is to prevent the
compiler's optimizer stage from assuming that the content
of a variable cannot change _asynchronously_ between
C statements.  The important word is ``asynchronously''.
It means changes to variables outside of the control flow
which is known to the compiler at compile-time.  This
includes signal handlers just as well as hardware registers,
shared memory etc.

I think the explanation which Giorgos gave is pretty good.

 > Could you explain me how it helps in your example? I pointed, that
 > you can get signal between two assembler commands and it
 > does destroys all your assumptions.

The assembler commands do not matter.  Only C statements
matter, because this is the smallest separable execution
unit of a C program.

Without "volatile", the programmer would be unable to write
his program in such a way that asynchronous changes work
with register optimizations.  This is why "volatile" exists,
and this is why a compiler _must_ obey it.

All of the above is (formally) specified in the standard,
I'd suggest that you read it carefully.

Regards
   Oliver

-- 
Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany
(Info: finger userinfo:olli@dorifer.heim3.tu-clausthal.de)

"In jedem Stück Kohle wartet ein Diamant auf seine Geburt"
                                         (Terry Pratchett)


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200001151950.UAA86770>