Re: Should I use mutex in this context?
Tommy wrote:
Ben Voigt [C++ MVP] wrote:
void MyThreads()
{
while (!PoorManShutdownSignal) {
.. thread loop ..
}
Trouble is, the compiler sees that PoorManShutdownSignal is not
modified inside the loop, so it optimizes this to:
if (!PoorManShutdownSignal) {
do {
...
} while (true);
}
You are kidding? why would the compiler fundamentally change this
logic? How would it KNOW the difference?
I see that is a fundamental problem, unrelated the desire goal which
will drastically alter behavior and simply doesn't match the original
logic at all.
What you are implying that any syntax like so:
while (someglobal) { ... }
will be changed to:
if (someglobal) do { ...} while(true;
and I fail to see the logical fundamental reasoning behind such a
major change in logic.
I really hope this is not true. I simply can't believe this.
There is no change in logic (as long as the compiler can prove that
someglobal isn't changed by the loop body or any function it calls -- it
pays no attention to other threads). The compiler has preserved the
semantics of *sequential* execution as required.
Think about this one:
int a[1000]
for( int i = 0; i < x * y + z - 72; i++ ) {
a[i] = i;
}
The compiler changes this to:
int a[1000];
const int u = x * y + z - 72;
for( int i = 0; i < u; i++ )
a[i] = i;
saving a multiplication, addition, and subtraction on every iteration of the
loop. Of course if the body of the loop changes x, y, or z then the
compiler can't do this any more.