Re: mutex question



Slava M. Usov wrote:
"Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx> wrote in message
news:eIQi89sbGHA.1276@xxxxxxxxxxxxxxxxxxxxxxx

[...]


This is why the 'volatile' keyword exists -- to tell the compiler
that a variable can change unexpectedly, such as when it is shared
between threads. Today's compilers are smart, but they can't read
your mind (yet). :)

That is *not* what volatile is for - multithreading is not part of its
utility, rather hardware interaction is.


The original statement is exactly correct: "a variable can change
unexpectedly", so the compiler must not optimize it away. "Hardware
interaction" is just as wrong as "multithreading".

However, hardware interaction is part of volatile's remit (as I said), whereas multithreading isn't (although it has been hijacked for this purpose by some, since it works under certain conditions).

On the contrary, it is *not* necessary nor is it sufficient to use
volatile on variables shared between threads.


On certain platforms, volatile is perfectly sufficient. Incidentally, one of
these platforms is a de facto standard for win32.

Even on those platforms (e.g. x86), volatile is still only sufficient for single reads and writes to aligned 32 bit values, and similar examples (such as fixing the ordering of two writes with respect to reads in another thread).

Tom
.



Relevant Pages

  • Re: mutex question
    ... Today's compilers are smart, but they can't read ... That is *not* what volatile is for - multithreading is not part of its ... utility, rather hardware interaction is. ... On certain platforms, volatile is perfectly sufficient. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Question about Delphi versus other languages
    ... And what compilers do you use? ... How do you do it for gcc then that has an external one? ... The products feel alien on all platforms. ... Would only work with consent of Borland as the major player by far. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Newbie Question
    ... > HLL) source program being run through compilers on different platforms ... > (Windows, UNIX, Macintosh) to produce a binary with the same functionality. ... Do compilers interpret the same way? ...
    (comp.lang.cpp)
  • Re: 5th anniversary of 3.4.3, ideas
    ... targetting a small, bound set of compilers and systems, it's ... with folks on WinDOS platforms who thought to have to program (or did ... nothing that makes me feel comfortable when talking about portability. ... have to install a specific runtime environment of a certain version. ...
    (rec.games.roguelike.nethack)
  • Re: math.nroot [was Re: A brief question.]
    ... >>> available AFAICT on the platforms I actually use (doesn't mean it ... >> It's entirely optional part of C99. ... Ah, but as I've said before, virtually all C compilers on 754 boxes ... This includes gcc before C99 ...
    (comp.lang.python)

Loading