The question is, in this case pf is itself volatile, so is the compiler allowed to assume that b won't change in the loop even if b is not volatile?
It can't, because you say that pf
might be changed by the other threads, and this indirectly changes b
if pf
is called then by the while loop. So while it is theoretically not required to read b
normally, it in practice must read it to determine whether it should short circuit (when b
becomes false
it must not read vb
another time).
Answer to the second part
In this case pf
is not volatile anymore, so the compiler can get rid of it and see that f1
has an empty body and f2
sets b
to false. It could optimize main
as follows
int main()
{
// threads here (which you say can only change "vb")
while(vb)
{
int x;
std::cin >> x;
if(x != 0)
break;
}
}
Answer to older revision
One condition for the compiler to be allowed to optimize the loop away is that the loop does not access or modify any volatile object (See [stmt.iter]p5 in n3126). You do that here, so it can't optimize the loop away. In C++03 a compiler wasn't allowed to optimize even the non-volatile version of that loop away (but compilers did it anyway).
Note that another condition for being able to optimize it away is that the loop contains no synchronization or atomic operations. In a multithreaded program, such should be present anyway though. So even if you get rid of that volatile
, if your program is properly coded I don't think the compiler can optimize it away entirely.