From owner-freebsd-questions Sat Jan 15 11:50:26 2000 Delivered-To: freebsd-questions@freebsd.org Received: from dorifer.heim3.tu-clausthal.de (dorifer.heim3.tu-clausthal.de [139.174.243.252]) by hub.freebsd.org (Postfix) with ESMTP id B55C515043 for ; Sat, 15 Jan 2000 11:50:23 -0800 (PST) (envelope-from olli@dorifer.heim3.tu-clausthal.de) Received: (from olli@localhost) by dorifer.heim3.tu-clausthal.de (8.9.3/8.9.3) id UAA86770; Sat, 15 Jan 2000 20:50:22 +0100 (CET) (envelope-from olli) Date: Sat, 15 Jan 2000 20:50:22 +0100 (CET) Message-Id: <200001151950.UAA86770@dorifer.heim3.tu-clausthal.de> From: Oliver Fromme To: freebsd-questions@FreeBSD.ORG Reply-To: freebsd-questions@FreeBSD.ORG Subject: Re: Volatile variables X-Newsgroups: list.freebsd-questions In-Reply-To: <85kinv$2ffa$1@atlantis.rz.tu-clausthal.de> User-Agent: tin/1.4.1-19991201 ("Polish") (UNIX) (FreeBSD/3.4-19991219-STABLE (i386)) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Mikhail Evstiounin wrote in list.freebsd-questions: > From: Oliver Fromme >>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