You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been thinking about a lightweight alternative to ThreadController that would both require less typing from the programmer and use less RAM. Here is what I have come with, which I call ThreadList:
template<int N> structThreadList {
Thread threads[N];
voidoperator()() {
unsignedlong now = millis();
for (int i = 0; i < N; i++)
if (threads[i].shouldRun(now)) threads[i].run();
}
};
As an example usage, say we have a task foo() to run every 200 ms, and a task bar() to run every 500 ms. With the ThreadController class, this would be done as follows:
With a ThreadList, there is no need to create a named variable for each Thread, nor to call the add() method of the controller. Instead, everything is handled by a single aggregate initialization:
ThreadList<2> runner = {
Thread(foo, 200),
Thread(bar, 500)
};
voidsetup() { } // nothing to do herevoidloop() {
runner();
}
The only drawback in the syntax above is that one has to explicitly give the size of the list as a template argument.
The other benefit of ThreadList is that it consumes 0 bytes of RAM in excess of the Threads it contains, whereas a ThreadController is 51 bytes. It can make a difference on the smallest chips.
Now, this has its own limitations. One of them is that, if one needs to enable or disable one Thread, or change its interval, the syntax becomes clumsy:
The cons I see:
You cannot place a ThreadList inside a ThreadList or inside a ThreadController, since it doesn't extends Thread.
I thought in another approach to that, that for sure will not reduce to 0, but will reduce memory. One problem that causes the size to be 51 bytes, is that we ares storing an array (by default) of 15 Threads (I'm not sure how much a pointer occupies, but if that's 2 bytes per pointer, than we have 30 bytes), a solution would be passing the number of Maximum Threads with the ThreadController, we could allocate memory just for that.
One more thing: I'm really thinking in changing the attribute enabled to something more extendable such as enabled(), and to set, use enabled(true|false).
Activity
ivanseidel commentedon Jun 17, 2015
I like two things in it, very much:
.run(), justmythread().The cons I see:
You cannot place a
ThreadListinside aThreadListor inside aThreadController, since it doesn't extendsThread.I thought in another approach to that, that for sure will not reduce to 0, but will reduce memory. One problem that causes the size to be 51 bytes, is that we ares storing an array (by default) of 15 Threads (I'm not sure how much a pointer occupies, but if that's 2 bytes per pointer, than we have 30 bytes), a solution would be passing the number of
Maximum Threadswith the ThreadController, we could allocate memory just for that.One more thing: I'm really thinking in changing the attribute
enabledto something more extendable such asenabled(), and to set, useenabled(true|false).CAHEK7 commentedon Aug 30, 2016
What about some C++11 template magic? This example doesn't have the main cons, so you can use that list everywhere, even inside
ThreadController.This is just a proof of concept. I can make proper implementation if the idea is interesting.
ivanseidel commentedon Mar 1, 2017
Closing because it's already Merged