Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Works on debug but not release

I have a thread that sets a value to true when it is done. Until then I wait:

while(1)
{
    if(done[0] == true)
    {

        break;
    }
}

This code works just fine in Debug but in Release it stays in the loop forever even though the debugger clearly says that it is true and not false.

Why would this not work?

Thanks

like image 869
jmasterx Avatar asked Nov 23 '25 09:11

jmasterx


1 Answers

This is symptomatic of not marking done as volatile.

Without volatile the optimising compiler can cache the value in a register.

e.g.

private volatile int i;
like image 157
Mitch Wheat Avatar answered Nov 24 '25 23:11

Mitch Wheat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!