Aussie AI
Detecting Spinning Loops
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Detecting Spinning Loops
Note that the above call-counting macro doesn't work for checking that a loop isn't spinning.
It might seem that we can use the above macro at the top of the loop body
to avoid a loop iterating more than 1,000 times.
But it doesn't work, because it will count multiple times that the
loop is entered, not just a single time.
If we want to track a loop call count, the counter should not be a “static
” variable,
and it's more difficult to do in a macro.
The simplest method is to hand-code the test:
int loopcount = 0; while (...) { if (++loopcount > 1000) { // Spinning? // Warn... } }
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |