V712. Be advised that compiler may delete this cycle or make it infinity. Use volatile variable(s) or synchronization primitives to avoid this.
The analyzer has detected a loop with an empty body which may be turned into an infinite loop or removed completely from the program text by the analyzer in the course of optimization. Such loops are usually used when awaiting some external event.
bool AllThreadsCompleted = false; // Global variable .... while (!AllThreadsCompleted);
In this case, the optimizing compiler will make the loop infinite. Let's take a look at the assembler code from the debug version:
; 8 : AllThreadsCompleted = false; mov BYTE PTR ?AllThreadsCompleted@@3_NA, 0 ; AllThreadsCompleted $LN2@main: ; 9 : ; 10 : while (!AllThreadsCompleted); movzx eax, BYTE PTR ?AllThreadsCompleted@@3_NA ; AllThreadsCompleted test eax, eax jne SHORT $LN1@main jmp SHORT $LN2@main $LN1@main:
The check is evidently present here. Now let's look at the release version:
$LL2@main: ; 8 : AllThreadsCompleted = false; ; 9 : ; 10 : while (!AllThreadsCompleted); jmp SHORT $LL2@main
Now the jump was optimized into a non-conditional one. Such differences between debug and release versions are often a source of complicated and hard-to-detect errors.
There are several ways to solve this issue. If this variable is really meant to be used to control the logic of a multi-threaded program, one should rather use the operating system's synchronization means such as mutexes and semaphores. Another way of fixing it is to add the 'volatile' modifier to the variable declaration to prohibit optimization:
volatile bool AllThreadsCompleted; // Global variable .... while (!AllThreadsCompleted);
Check the corresponding assembler code in the release version:
$LL2@main: ; 9 : ; 10 : while (!AllThreadsCompleted); movzx eax, BYTE PTR ?AllThreadsCompleted@@3_NC ; AllThreadsCompleted test al, al je SHORT $LL2@main
However, the V712 diagnostic message sometimes "misses the target" and points to those fragments where no infinite loop should exist at all. In such cases, an empty loop is probably caused by a typo. Then this diagnostic often (but not always) intersects with the V715 diagnostic.
This diagnostic is classified as:
You can look at examples of errors detected by the V712 diagnostic.