volatile int vfoo = 0;
void func()
{
int bar;
do
{
bar = vfoo; // L.7
}while(bar!=1);
return;
}
This code busy-waits for the variable to turn to 1
. If on first pass vfoo
is not set to 1
, will I get stuck inside.
This code compiles without warning. What does the standard say about this?
vfoo
is declared asvolatile
. Therefore, read to this variable should not be optimized.- However, bar is not
volatile
qualified. Is the compiler allowed to optimize the write to thisbar
? .i.e. the compiler would do a read access tovfoo
, and is allowed to discard this value and not assign it tobar
(at L.7). - If this is a special case where the standard has something to say, can you please include the clause and interpret the standard's lawyer talk?
bar
"? In order to do that, the value it's writing would have to be known in advance, but the value is the result of loadingvfoo
, which cannot be elided becausevfoo
is volatile. – Resentmentbar
doesn't matter, compiler isn't allowed to optimize the read ofvfoo
. 3) No it is not a special case and 100% well-defined. – Booze