Re: mutex question



Stealer wrote:
// ???? while(x); <> while(x) { statements} ???
// VC Compiler bug in Release configuration

[...snip...]

continue_flag = 1;
CreateThread(NULL, 0, ThreadProc, 0, 0, 0);
while (continue_flag)
{
//printf("You : I love you, Bill\n"); // you can not exit this program
if remove this line.
};

[...snip...]

// Note: You must buid in Release to find out the problem
// Disassembly:
// 00401048 jmp 00401048
// --> no compare before jmp

This is not a compiler bug -- this is a code bug. The continue_flag
variable should be declared as volatile since it can be modified by
another thread. Since you're not changing continue_flag in the loop,
it is perfectly valid for the compiler to optimize the check away
completely.

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). :)

Why does it not optimize the check away when a call to printf is in
the loop? Maybe since it's a global variable and you're calling a
function in another compilation unit, it thinks that the variable
might be extern'ed and potentially modified by that function. I'd
bet that if you replaced the statement with, say, setting the value
of a local variable, you'd get the optimized code -- just jmp. (It
would probably move the assignment out of the loop.)

Long story short: use 'volatile' on variables shared between threads.

David
.



Relevant Pages

  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... >> both less efficient and less safe than the Fortran and Basic standard. ... >> The C for loop is actually trying to do what a do loop does. ... sloppy thinking that results from confusing a programming language ... > I do not believe that you are capable of writing a conforming C compiler. ...
    (comp.programming)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... it's a for loop in the C sense. ... > sloppy thinking that results from confusing a programming language ... >> I do not believe that you are capable of writing a conforming C compiler. ... Does Microsoft's C compiler perform this optimisation? ...
    (comp.programming)
  • Re: Histogram of character frequencies
    ... generated object code may simply be a loop in which elements are ... believe any C compiler anywhere would reject it. ... On the first iteration of the loop you test the end of file indicator ...
    (comp.lang.c)
  • Re: [patch] spinlocks: remove volatile
    ... and somebody added "volatile" to hide the problem). ... The 'C' compiler has no choice but to actually make that memory access ... 'knows' that spinner is FALSE, ... The compiler can only optimize away that loop if spinner is provably const. ...
    (Linux-Kernel)
  • Re: Fridays the thirteenth. (And a little puzzle.)
    ... -- compiler) is the usual method ... int febdays ... -- We're going to go round a loop dealing with each year in turn. ... -- other languages call) ...
    (uk.people.silversurfers)

Loading