Closed CAHEK7 closed 7 years ago
Not sure why I did like this, but perhaps to reduce millis() calls. I might have thought that that displacement in time could disorganise threads with time.
Agreed that millis()
can be heavy, but if you have 2 threads close to each other (for example we have 100ms and 110ms intervals) and the first thread's run function takes more than 10ms to execute, we will skip the second one each first cycle.
True, but this can also happen (and with more frequency because Threads are supposed to run within a short a ammount of time, usually < 5ms): You create 10 threads with 100ms delay on each other. After some time, they get dessincronized by the time each one takes to run.
In the current way, if you add two threads that should run on a fixed period, they will always be executed in the same order, and in the same controller.run()
In the code below when you cache
time
before the loop andthread[i]->run();
takes more time than interval to the next thread - the next thread will not be run in thatrun
cycle.This is not so critical for code like this:
because we run the next cycle right after the previous, but if
run
function attached to the timer interrupt we may miss whole timer cycle. This becomes more critical if the timer frequency is low.I guess it would be better to replace
if(thread[i]->shouldRun(time))
withthread[i]->shouldRun())
which is implied to callmillis()
each time.What do you think?