Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When to use the 'volatile' keyword ?

19 views
Skip to first unread message

Alexander Terekhov

unread,
Nov 6, 2002, 5:09:14 PM11/6/02
to

< a bit of pure google-ing this time, I'm sorry, Dragan. >

Carlos Moreno wrote:
>
> Ron wrote:
> >
> > I *know* that my response will restart a long-simmering debate over
> > the adequacy of "volatile", the merits of pthreads, and the nature of
> > the universe
>
> For an interesting discussion about the nature of the universe,
> you (I mean, the OP) can check the thread I started a few weeks
> ago in the newsgroup comp.programming.threads, with subject
> "Talking about volatile and threads synchronization."
>
> After 65 articles, I think the conclusion was that depending
> on the threads library that you use, you *may* need to use
> volatile *in addition* to proper synchronization mechanisms.
>
> With POSIX threads, as Ron indicated, there is absolutely no
> need to use volatile (trust me: read the 65 articles in that
> thread :-)).
>
> Volatile in general avoids optimizations concerning skipping
> a read to a variable because the compiler assumes its value
> by inspecting the surrounding code. This is particularly
> useful when reading memory-mapped hardware registers, for
> which the contents may change in a way completely beyond
> the control or the awareness of the executing program. But
> for threads (at least with POSIX threads), apparently is
> not that useful, since you *must* use synchronization
> techniques anyway (otherwise accesses to even an int could
> end up conflicting and leaving the int in an inconsistent
> state, if the timing is right -- I mean profoundly wrong :-)),
> and synchronization techniques save you also from the problem
> that volatile saves you from...
>
> Anyway, go to http://groups.google.com, and then type the
> subject I indicated above (make sure you enclose it in
> double-quotes) for more details.
>
> HTH,
>
> Carlos
> --
>
> [ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
> [ about comp.lang.c++.moderated. First time posters: do this! ]

< Forward Quoted >

c++-r...@netlab.cs.rpi.edu schrieb am 06.11.02 22:09:39:
> Message-ID: <3DC84D27...@web.de>
> Newsgroups: comp.lang.c++.moderated
> From: Alexander Terekhov <tere...@web.de>
> Reply-To: tere...@web.de
> Subject: Re: When to use the 'volatile' keyword ?
> Organization: unknown
> References: <b9a051e6.02110...@posting.google.com>
> <c668c1f8.02110...@posting.google.com>
> <7AUx9.10262$7e4.2...@weber.videotron.net>
> X-Original-Date: Tue, 05 Nov 2002 23:58:47 +0100
> X-Submission-Address: c++-s...@netlab.cs.rpi.edu
>
> {There was no new technical content added to the quoted article
> (I-agrees, me-toos, and repasting from other articles fall into
> this category). Please add some technical C++ content and
> resubmit. Thanks! -mod}
>
>
> Carlos Moreno wrote:
> >
> > Ron wrote:
> > >
> > > I *know* that my response will restart a long-simmering debate over
> > > the adequacy of "volatile", the merits of pthreads, and the nature
> of
> > > the universe
> >
> > For an interesting discussion about the nature of the universe,
> > you (I mean, the OP) can check the thread I started a few weeks
> > ago in the newsgroup comp.programming.threads, with subject
> > "Talking about volatile and threads synchronization."
> >
> > After 65 articles, I think the conclusion was that depending
> > on the threads library that you use, you *may* need to use
> > volatile *in addition* to proper synchronization mechanisms.
>
> Please read it [the entire 65 articles] again, Carlos.
>
> Here's one more article: < Forward Inline, you'll probably need to
> manually insert "3D" after "=" to make the links work again... due
> to BROKEN "moderation" in this newsgroup -- software, I mean ;-) >
>
> -------- Original Message --------
> Message-ID: <3DAD73CB...@web.de>
> Newsgroups: comp.lang.c++,alt.comp.lang.learn.c-c++
> Subject: Re: "discards volatile" warnings, how to get rid of them
> correctly?
>
> Paavo Helde wrote:
> [...]
> > POD types have relaxed rules in order to not break C compatibility.
> Thus
> > "B.a = A.a" is allowed. If the hardware happens to ensure atomic
> reads,
> > it is even correct.
>
> Single threaded static-volatile-sig_atomic_t aside, C/C++ volatile type
> specifier has really nothing to do with atomicity [and/or memory
> visibility w.r.t. thread-/process- shared read/write access]... unless
> you're talking about implementation-specific hacks ala DEC/Compaq/NewHP
> "strong volatile" thing to fight "word tearing" race condition.
>
> > It seems to be established that 'volatile' is actually only required
> for
> > memory-mapped hardware registers.
>
> Almost correct[1] (longjump()ing and sig_atomics aside).
>
> > In multithreading issues one should
> > use locking anyway, and locking introduces memory barriers which
> ensure
> > that memory is read/written properly. However, 'volatile' may be handy
> > as an orthogonal keyword to 'const' as ensure something-correctness.
> > Andrei Alexandrescu has implemented this idea for checking
> > 'locking-correctness' (http://www.cuj.com/experts/1902/alexandr.htm).
> By
> > a coincidence this usage is also related to multithreading, ....
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> "this usage" is nothing but *undefined behavior* (to begin with[2]).
>
> regards,
> alexander.
>
> [1] http://groups.google.com/groups?threadm=A7FFA2.515FF640%40web.de
> (Subject: Re: Talking about volatile and threads synchronization...)
>
> [2] http://groups.google.com/groups?threadm=99EF5F.B9D8C5B8%40web.de
> (Subject: Re: volatile -- what does it mean in relation to member
> functions?)
>
> [ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
> [ about comp.lang.c++.moderated. First time posters: do this! ]

regards,
alexander.

Alexander Terekhov

unread,
Nov 6, 2002, 5:11:26 PM11/6/02
to

< another bit of pure google-ing, I'm DEEPLY sorry this time, Dragan >

Alexander Terekhov wrote:
>
> Shannon Barber wrote:
> >
> > p...@21cn.com (pbi) wrote in message news:<b9a051e6.02110...@posting.google.com>...
> > > Should I use the 'volatile' type qualifier when declaraing variable
> > > which will be accessed by multithread?
> > > For example:
> > >
> > > int flag=1;
> > >
> > > fun1()
> > > {
> > > while(flag)
> > > {
> > > }
> > > }
> > >
> > > fun2()
> > > {
> > > flag=0;
> > > }
> > >
> > > fun1 and fun2 are executed in different threads. If I don't use
> > > volatile to modify the variable flag's definition, compiler might do
> > > some optimizations that
> > > cause the loop in fun1 infinite. So I think I should modify the first
> > > line to
> > > volatile int flag=1;
> >
> > This is the C textbook example use of volatile - it's not a generally
> > recommended practice, but it demonstrates the issue volatile
> > addresses.
>
> C/C++ volatile DOESN'T address any issues related to THREADING.
>
> > > But how about if flag is not a primitive type variable? If its type is
> > > a class, or if it's a member of a class, should I use the volatile
> > > keyword?
> > >
> > For classes volatile behaves much like const; you can only invoke
> > methods tagged (what's the official term for the cvq keyword after a
> > method defintion?) with volatile. You can also cast away the
> > volatile'ness with const_cast (say, after acquiring mutex).
>
> Mama mia. That's UNDEFINED BEHAVIOR [if you access it]. End of story.
>
> Well, more on this:
>
> http://groups.google.com/groups?threadm=ajaqne%24n9u%241%40glue.ucr.edu
> (comp.std.c, Subject: concurrency-induced memory-access anomalies)
>
> http://groups.google.com/groups?threadm=A35E63.5050200%40xx.xxx
> (c.p.t., Subject: Talking about volatile and threads synchronization...)
>
> http://groups.google.com/groups?selm=BFF494.5FB11101%40web.de
> (comp.std.c, Subject: Re: Volatile automatic variables and setjmp)
>
> regards,
> alexander.


>
> [ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
> [ about comp.lang.c++.moderated. First time posters: do this! ]

< Forward Quoted >

c++-r...@netlab.cs.rpi.edu schrieb am 06.11.02 22:06:41:
> Message-ID: <3DC84830...@web.de>


> Newsgroups: comp.lang.c++.moderated
> From: Alexander Terekhov <tere...@web.de>
> Reply-To: tere...@web.de
> Subject: Re: When to use the 'volatile' keyword ?
> Organization: unknown
> References: <b9a051e6.02110...@posting.google.com>

> <de001473.02110...@posting.google.com>
> <3DC811DE...@web.de>
> X-Original-Date: Tue, 05 Nov 2002 23:37:36 +0100
> X-Submission-Address: c++-s...@netlab.cs.rpi.edu
>
> {This article seems to be intended for the moderators
> rather than the newsgroup itself. To send mail to the
> moderators, use c++-r...@netlab.cs.rpi.edu. That list
> is cc'd on rejections, so they'll see this and be able
> to reply. -mod}
>
>
> Alexander Terekhov wrote:
> [...]
> > http://groups.google.com/groups?threadm=A35E63.5050200%40xx.xxx
> > (c.p.t., Subject: Talking about volatile and threads
> synchronization...)
> >
> > http://groups.google.com/groups?selm=BFF494.5FB11101%40web.de
> > (comp.std.c, Subject: Re: Volatile automatic variables and setjmp)
>
> Add [discarded by c.l.c++.MOD-soft] "3D" after "=". To mods: this is
> totally BROKEN newsgroup, really. Why don't you fix the soft so that
> it would NOT tend to eliminate "3D" from "...=" "3D..." literals.
> Please.
>
> regards,
> alexander.

Alexander Terekhov

unread,
Nov 7, 2002, 12:14:06 PM11/7/02
to

< Dragan, this one is for you ;-) ;-) Googling... >

Garry Lancaster wrote:
>
> pbi:


> > Should I use the 'volatile' type qualifier when declaraing variable
> > which will be accessed by multithread?
>

> The C++ standard does not deal with multiple threads of
> execution. So, the answer is "it depends on your threading
> environment".
>
> POSIX threads (pthreads) rules are that the volatile qualifier
> is neither necessary nor sufficient for safe multi-thread
> access: in fact its main effect is to act as a pessimization
> inhibiting many harmless compiler optimizations.
>
> Windows threading rules are, as far as I know, not spelled
> out anywhere explicitly so it depends on the hardware you
> are running on. Win32 on x86 is a more permissive threading
> environment than pthreads (i.e. you can get away with more),
> but Alpha (yes, Windows was ported to the Alpha) is fairly
> similar to the pthreads model and I'm guessing Win64
> hardware will be more Alpha-like in this respect. So, I would
> still generally recommend sticking with the pthreads memory
> visibility model. It is quite close to the minimum that would
> actually work, so you will if anything be erring on the side of
> caution.
>
> In summary, your example as written, either with or without
> the volatile qualification, is a very non-portable way of doing
> things: there are a lot of platforms where it will not work.
> If you protect all accesses to the flag with a mutex (a.k.a
> CRITICAL_SECTION on Windows) you will have a very
> portable solution (at least conceptually: in practice the syntax
> for initializing, locking, unlocking and destroying a mutex
> differs between threading systems although Boost.Threads
> abstracts away these differences for all systems it supports)
> Their are likely other solutions that sit somewhere in between.
>
> For more information...
>
> Boost.Threads is a library at www.boost.org.
>
> The best book on pthreads is probably David Butenhof's one.
> Even if you don't use pthreads, it is conceptually very clear
> about how threading works.
>
> Windows threading documentation is at msdn.microsoft.com.
>
> Also see the comp.programming.threads FAQ. If you are still stuck,
> you will probably find more threading people on that newsgroup
> than this one.
>
> Kind regards
>
> Garry Lancaster
> Codemill Ltd
> Visit our web site at http://www.codemill.net


>
> [ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
> [ about comp.lang.c++.moderated. First time posters: do this! ]

< Forward Quoted >

c++-r...@netlab.cs.rpi.edu schrieb am 07.11.02 17:44:07:
> Message-ID: <3DC9B6A6...@web.de>


> Newsgroups: comp.lang.c++.moderated
> From: Alexander Terekhov <tere...@web.de>
> Reply-To: tere...@web.de
> Subject: Re: When to use the 'volatile' keyword ?
> Organization: unknown
> References: <b9a051e6.02110...@posting.google.com>

> <HcTx9.1239$3l2.142114@newsfep2-gui>
> X-Original-Date: Thu, 07 Nov 2002 01:41:10 +0100
> X-Submission-Address: c++-s...@netlab.cs.rpi.edu
>
> {This part of the thread has drifted off-topic for clc++m.
> Please redirect followups to the appropriate system-specific
> or advocacy newsgroups, or to email. Thanks! -mod}
>
>
> Garry Lancaster wrote:
> [...]
> > Windows threading documentation is at msdn.microsoft.com.
>
> Yeah, but don't miss this:
>
> http://sources.redhat.com/ml/pthreads-win32/2001/msg00158.html
> (Subject: Re: new to pthreads)
>
> <quote>
>
> I am just curious whether someone from
> "appropriate Microsoft group" really spend
> a minute or two for review of
>
> </quote>
>
> regards,
> alexander.
>
> --
> "The entire MSDN stuff on threading [except one rather old article
> about IOCPs] is totally broken and utterly brain-dead."
>
> -- <http://tinyurl.com/2hto>

0 new messages