Delay (traditionally) has two functions that it performs:
- Wait for a period
- Yield processing to other threads through the
yield()
weak symbol.
By using a delay(0)
the author thinks they are saying "I don't want to delay here, but if anything is using the yield()
function it can run now."
However, that is not correct.
The code for delay()
in the AVR core looks like this:
void delay(unsigned long ms)
{
uint32_t start = micros();
while (ms > 0) {
yield();
while ( ms > 0 && (micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}
You can see that, as well as delaying for a period, yield()
is regularly called. However that only ever happens if ms
is greater than 0. If you pass 0
as the delay length then the delay()
function essentially does nothing at all.
Now it could be that an earlier version of the core had yield()
always being called even with ms==0
, but the current version doesn't. Maybe this is a hangup from an old coding style. But for the current core if you want to yield you must explicitly call yield()
or delay for at least one millisecond.