Re: mutex question
- From: "Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx>
- Date: Thu, 04 May 2006 14:52:50 +0100
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
.
- Follow-Ups:
- Re: mutex question
- From: Slava M. Usov
- Re: mutex question
- References:
- Re: mutex question
- From: David Jones
- Re: mutex question
- From: Tom Widmer [VC++ MVP]
- Re: mutex question
- From: Slava M. Usov
- Re: mutex question
- Prev by Date: Re: mutex question
- Next by Date: DDK SMP Build
- Previous by thread: Re: mutex question
- Next by thread: Re: mutex question
- Index(es):
Relevant Pages
|
Loading