Re: multiple threads and volatile
- From: Hallvard B Furuseth <h.b.furuseth@xxxxxxxxxxx>
- Date: Thu, 10 Jul 2008 21:48:48 +0200
S James S Stapleton writes:
or maybe you are not smart enough to figure out why the optimization
would not always be correct.
Smart and knowledgeable are two separate things. In this case the
matter is completely about knowledge, something that I am attempting
to obtain. I don't mind you being less than gentle, but please keep
your harsh realities appropriate 'ignorant' would be the correct term
which I'm attempting to alleviate myself of.
Sorry, I mean that, it was just a cut&paste of the previous uses of
"smart" (about the compiler). Though actually "not smart enough" can be
a real problem in any case. A number of details of C's semantics are
hairy enough that even the authors of the C standard get it wrong now
and then or can't figure out exactly what it implies.
(...) I just read that volatile is useful in C to help with proper
access of variables, but the document didn't describe /why/. I've been
trying to figure out why since that point. Everything I saw seemed to
indicate that it ensures you aren't using a register value for the
variable. (...)
More or less. Looks like you were redirected away from comp.lang.c too
early. What is means, formally, is roughly this:
- A compiled C program must behave exactly as the program spells out,
step for step, with some exceptions. E.g. if you write
i = 150;
i += 50;
then the program must behave as if it did just that rather than
i = 200;
- The compiler can optimize that to i = 5; if it can prove either that
this _will_ behave exactly the same way, or that it can only behave
differently if the behavior is not well-defined (in standardese, if
the program would have "undefined behavior" or "unspecified
behavior").
- However declaring an object volatile tells the compiler that access to
it is a "side effect" (like file I/O). Side effects must take place
just like the program spells out: Store 150, read it back (which for
all the compiler knows might give a different result than what it just
stored), then add 50 and store the result again.
- There is no absolute ordering however. The C language defines
"sequence points" at which each side effect must be complete: At ';'
and ',', at function calls, and some other places. Thus volatile
doesn't help with the infamous 'i = i++;' - that has undefined
behavior because there is no sequence point between the 'i=' and 'i++'
which says which should store 'i' first.
- Also the C language spec doesn't know about threads, or multi-core
CPUs and the like. So:
This would strike me as important if two processors are using that
variable, even if locks are stopping them from simultaneous use, I
don't know of any way to ensure the variable's value is ensured to be
copied from memory to register, or from register to memory.
Right, as far as the C language is concerned ugly things can happen anyway.
In practice, thread primitives like pthread_mutex_lock() ensure cache
coherency and the like. (E.g. ensuring that if two memory locations
are updated, two threads on different CPUs see the updates in the same order.)
Just declaring a variable volatile might not, I'm not quite sure.
http://en.wikipedia.org/wiki/Cache_coherency
Threading memory models are being actively discussed at least in the C++
community, it seems hard to nail down a definite model.
--
Hallvard
.
- Follow-Ups:
- Re: multiple threads and volatile
- From: Hallvard B Furuseth
- Re: multiple threads and volatile
- References:
- multiple threads and volatile
- From: S James S Stapleton
- Re: multiple threads and volatile
- From: Hallvard B Furuseth
- Re: multiple threads and volatile
- From: S James S Stapleton
- multiple threads and volatile
- Prev by Date: Re: multiple threads and volatile
- Next by Date: Re: multiple threads and volatile
- Previous by thread: Re: multiple threads and volatile
- Next by thread: Re: multiple threads and volatile
- Index(es):
Relevant Pages
|