We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am making a project that relies on the thread("..."); function extensively, and I want to find a way to make one of these threads to stop in the middle of the code without eating CPU.
The thing I am currently using is this:
void lag(float time){
int millis=millis();
while(millis()<millis+time/1000){float wow=floor(sin(0));}
}
This makes me able to run function lag(5); and that will make it wait 5 seconds, however this also eats a lot of CPU while it waits.
Or I can spam loadStrings("https://www.google.com"); a couple of times, which does not eat CPU, but is too unpredictable.
Is there a function that will work the same as my lag() function, but does not actually consume CPU?
Answers
https://Processing.org/reference/delay_.html
Thanks!
For future readers, it should be mentioned that this is a perfect use for delay -- and almost every other use for delay is terrible!
One very common use of
delay()
is when you create separate threads with infinite tight loops. Withoutdelay()
such a thread can hog the CPU making the sketch unresponsive.This sketch has two infinite loops each inside there own thread.