-->
Page 1 of 2

yield() and delay() best practices

PostPosted: Sun Feb 04, 2018 4:17 pm
by renegade
Hi all,

I keep hearing about yield, and delay();

delay() is obvious, it sleeps the main thread after the specified time it jumps back to work.

yield() appears to work background processes.

So, my question is, in my loop(), should I call both yield and delay? Or just delay?

Additionally I have heard the delay calls yield(), but I don't know if that's true! Advice and best practices please.

Re: yield() and delay() best practices

PostPosted: Sun Feb 04, 2018 4:20 pm
by rudy
yield() appears to work background processes.


So does delay. But i have never bothered to understand the differences. Maybe this time. :)

I found this https://github.com/esp8266/Arduino/issues/3042

Trying to digest it now.

Re: yield() and delay() best practices

PostPosted: Sun Feb 04, 2018 5:53 pm
by renegade
yep read that and got no conclusions, which is why i came here!

Re: yield() and delay() best practices

PostPosted: Sun Feb 04, 2018 7:29 pm
by McChubby007
There are 2 contexts, one that loop is called from, and the other one which is for the system/sdk/wifi etc.

Calling yield from the loop context saves the current stack and then executes the 'other' context, which will be the 'system' context, it then returns to the saved (loop) context when it has finished any pending work. That is why the link in the previous post states that yield cannot be called from the non-loop context, as that is not how it's designed to work as where would it yield to?

Delay calls yield until such time as the delay has expired and then returns to the delay() calling function.

That's how I understand it from my distant memory.