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

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++