Yun, Bridge library and runAsynchronously

bode:

  • The process variable is local to loop() but loop runs indefinitely so how does the process variable go out of scope? The loop() should continue where it left before the interrupt.

The code WILL continue when the interrupt returns. However, loop() does not RUN indefinitely, it is CALLED indefinitely. There is a difference that doesn't always matter, but in this case it does.

What's really happening behind the scenes is that there is a hidden main() function that essentially does this:

void main()
{
   setup(); // call setup once

   while (true)
   {
       loop(); // call loop over and over, forever
   }
}

Each time loop is called, a new Process instance is created. Sometimes (when the counter didn't change) loop quickly exits and the Process instance is destroyed. Other times, the counter has changed, so the process is started, and then almost immediately it is destroyed when loop() returns. It's getting called over and over, but the Process variable is indeed going out of scope on each iteration.

You could move the Process declaration outside to make it global, or you could keep it in loop() but make it static so that it isn't allocated and released on each iteration. Or you could add your own infinite loop inside of loop() so that loop() never returns.