Re: multiple threads and volatile



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
.



Relevant Pages

  • Re: "STL from the Ground Up"
    ... high-level intermediate language than can interoperate with many other ... If your language lacks expressive features then you cannot write code ... memory management in comparison. ... Mostly because type errors mean that the programmer and compiler disagree ...
    (comp.programming)
  • Re: Executable Memory in a Driver
    ... >> criminal to expose users to the added bluescreen and security risk. ... In a language that can't access outside an array, ... that doesn't need to move memory. ... > desired in the compiler. ...
    (microsoft.public.development.device.drivers)
  • Re: operation on module arguments
    ... But the logic to have this feature in the ... language is pretty reasonable, so I was not too greedy, was I? ... memory for storing a real variable, and let the variable name 'A' refer to that bit of memory." ... It means, for example, that the compiler can't pass A around like a real variable, because when the compiler passes it to a subroutine that might modify it, the compiler also has to pass along the instructions that go with it. ...
    (comp.lang.fortran)
  • Re: RFC : SOME IDEAS FOR THE APPLE II FPGAers
    ... After porting a compiler to a new machine, ... use the other half of the machine's memory for profile counters ... middling peaks for fast call expansion, and the rest for interpretation. ... etc. and hand craft the critical sections in assembly language. ...
    (comp.sys.apple2)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... An 'lvalue' is *not* any kind of value, ... function it allocates more memory and copies the literal to the new ... might keep a copy of the pointer, and store that pointer other ... and the C compiler will already forbid you ...
    (comp.lang.c)