At Wed, 21 Sep 2005 18:09:06 +0200, "Udo A. Steinberg" us15@os.inf.tu-dresden.de wrote:
From the example code you posted it doesn't look like you made the variable that is shared between the two threads "volatile". Only when that variable is volatile will the compiler read it from memory every time. Otherwise it is free to cache the variable in a register and may not see the write from the other thread.
I hate to distract from the real issue, but I should note that volatile does not do what you describe here. If you need to make sure that you see the write of another thread, you must use a memory barrier or another proper synchronization primitive. "volatile" is not the answer.
For an extensive analysis, please see for example sec. 5 in "C++ and the Perils of Double-Checked Locking":
http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
Thanks, Marcus