4 ms·
> This particular bug actually comes from C++. In C++, threads may be assumed to always make forward progress, i.e. side-effect-free infinite loops are UB. Howe
by lmm 7y ago
> This particular bug actually comes from C++. In C++, threads may be assumed to always make forward progress, i.e. side-effect-free infinite loops are UB. However, this is not true in C
That's very much disputed. The C standard requires programs' behaviour to be as-if executed on the C abstract machine when they terminate. It's not at all clear whether the standard imposes any requirements on programs that would not terminate when executed on the C abstract machine.
- _kst_ 7y agoC11 does have a special case rule for infinite loops. Section 6.8.5 paragraph 6: "An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate." (I'm personally not happy that this is stated in terms of that the implementation may assume rather than what a program may do.) Note that this does not apply to infinite loops with constant conditions. For example, given `while (1) { /* ... */ }`, the above doesn't apply and the implementation may not assume the loop will terminate.
- lmm 7y ago> Note that this does not apply to infinite loops with constant conditions. For example, given `while (1) { /* ... */ }`, the above doesn't apply and the implementation may not assume the loop will terminate. Where is that guaranteed? I can see that the above rule suggests that the implementation may not make that assumption, but it doesn't actually rule it out.
- pingyong 7y agoIs there even a case where the compiler can optimize a terminating loop better when it is allowed to assume that it will terminate at some point?
- eru 7y agoYes. The simplest example is when the loop has no other side-effects. But you could also have something like: while(true) { if(some condition) { do stuff; break; } } In this case, a compiler might be allowed to assume that 'some condition' happens to be true eventually.
- pingyong 7y agoOkay and which optimization does this allow for? What's the difference in the emitted binary?
- lmkg 7y agoIf you propagate "some condition," that could allow dead-code elimination later on, especially if an if-statement uses "some condition" as a predicate later. This seems redundant in hand-written code, but keep in mind the if-statement may actually be from an inlined function. If "some condition" is actually testing a variable for equality, then you could do constant-folding.
- pingyong 7y agoMy point is that if a loop - doesn't terminate, doesn't have side effects -> optimization doesn't matter - doesn't terminate, has side effects -> optimization isn't valid - does terminate, doesn't has side effects -> optimization would be valid, but why would such a loop exist? One example that was brought up was generic code, but I'm not entirely convinced yet.
- eru 7y agoBecause of interaction with other undefined behaviour (in the body of your loop), you might think you are in a different case than what the compiler thinks. Also the definition of 'side effect' here is a weird one: reading and writing to variables is not considered a side effect, even though a different thread could change them while your loop is running.