🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++.moderated
Article #32

Re: Meaning of the specifier volatile?

#32
From: John G. MacCulloch
Date: Fri, 05 Jan 1996 09:11
59 lines
3415 bytes
X-Original-Date: Fri, 05 Jan 96 06:16:22 GMT

In article <4cb5i3$5mr@netlab.cs.rpi.edu>,
   Dave Nulton <dnult@axiom.net> wrote:
>Path: news.interlog.com!winternet.com!uunet!in1.uu.net!news.mathworks.com!news.kei.com!ub!library.erc.clarkson.edu!rpi!not-for-mail
>From: Dave Nulton <dnult@axiom.net>
>Newsgroups: comp.lang.c++.moderated,comp.lang.c++
>Subject: Re: Meaning of the specifier volatile?
>Date: 2 Jan 1996 11:39:47 -0000
>Organization: Axiom Communications
>Lines: 24
>Sender: cppmods@netlab.cs.rpi.edu
>Approved: kanze@gabi-soft.fr
>Message-ID: <4cb5i3$5mr@netlab.cs.rpi.edu>
>References: <4c9740$27n@netlab.cs.rpi.edu>
>NNTP-Posting-Host: netlab.cs.rpi.edu
>Xref: news.interlog.com comp.lang.c++.moderated:518 comp.lang.c++:166027
>Status: N
>
>X-Original-Date: 2 Jan 1996 07:22:26 GMT
>
>I'm no expert but here is what I know.  The specifier volatile is 
>a directive to the compiler that the variable declared volatile 
>can change at any moment.  The compiler will then make no 
>assumptions about the contents of this variable, and will always 
>re-check its value when used.  An example of something that might 
>use volatile variables is:  a hardware interface to your PC which 
>reads analog data and converts it to digital.  Your program may 
>read the data from the PC bus.  However this data is always 
>changing and your program should make know assumptions that the 
>data is the same - it should always recheck the value.
>
>I'm sure there are other less complicated applications which may 
>benefit from volatile variables.  I'm sure one of the many other 
>experts on the net can elaborate.
>
>-dnult@axiom.net
>
>
>    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
>    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
>    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
>    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]


"volatile" can be handy in a multi-threaded program too.  Suppose you've got a flag that
will be set in one thread to indicate that another thread can now proceed.  The flag needs
to be marked as "volatile" because its value will magically change through an external 
source (i.e. the other thread).  Without the "volatile" keyword, an optimizing compiler is probably 
going to assume that the value of the flag can't change (resulting in an infinite loop -- not that 
this has ever happened to me).

that is waiting for the flag to be set, the The thread
that is waiting that indicates that another thread can 

    [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
    [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
    [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
    [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
Technical Details
Message-ID: <4cipvn$pma@netlab.cs.rpi.edu>
Path: arc1-ca.pugleaf.net!library.erc.clarkson.edu!rpi!not-for-mail
References: <4c9740$27n@netlab.cs.rpi.edu> <4cb5i3$5mr@netlab.cs.rpi.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!library.erc.clarkson.edu!rpi!not-for-mail
From: jmac@interlog.com (John G. MacCulloch)
Newsgroups: comp.lang.c++.moderated,comp.lang.c++
Subject: Re: Meaning of the specifier volatile?
Date: 5 Jan 1996 09:11:19 -0000
Organization: InterLog Internet Services
Sender: cppmods@netlab.cs.rpi.edu
Approved: kanze@gabi-soft.fr
Message-ID: <4cipvn$pma@netlab.cs.rpi.edu>
References: <4c9740$27n@netlab.cs.rpi.edu> <4cb5i3$5mr@netlab.cs.rpi.edu>
NNTP-Posting-Host: netlab.cs.rpi.edu