Skip to content

Commit 29b2db4

Browse files
committed
add StaticThreadController
1 parent fcd0b64 commit 29b2db4

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

StaticThreadController.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
StaticThreadController.h - Controlls a list of Threads with different timings
3+
4+
Basicaly, what it does is to keep track of current Threads and run when
5+
necessary.
6+
7+
StaticThreadController is an extended class of Thread, because of that,
8+
it allows you to add a StaticThreadController inside another kind of ThreadController...
9+
10+
It works exact as ThreadController except you can't add or remove treads dynamically.
11+
12+
Created by Alex Eremin, September, 2016.
13+
Released into the public domain.
14+
*/
15+
16+
#ifndef StaticThreadController_h
17+
#define StaticThreadController_h
18+
19+
#include "Thread.h"
20+
21+
template <int N>
22+
class StaticThreadController: public Thread{
23+
protected:
24+
Thread thread[N];
25+
public:
26+
template <typename... T>
27+
StaticThreadController(T&&... params) :
28+
Thread(),
29+
thread{params...}
30+
{
31+
#ifdef USE_THREAD_NAMES
32+
// Overrides name
33+
ThreadName = "StaticThreadController ";
34+
ThreadName = ThreadName + ThreadID;
35+
#endif
36+
};
37+
38+
// run() Method is overrided
39+
void run() override
40+
{
41+
for(int i = 0; i < N; i++){
42+
// Is enabled? Timeout exceeded?
43+
if(thread[i].shouldRun()){
44+
thread[i].run();
45+
}
46+
}
47+
48+
// StaticThreadController extends Thread, so we should flag as runned thread
49+
runned();
50+
}
51+
52+
// Return the quantity of Threads
53+
static constexpr int size() { return N; };
54+
55+
// Return the I Thread on the array
56+
// Returns nullptr if index is out of bounds
57+
Thread* get(int index) { return (index >= 0 && index < N) ? &thread[index] : nullptr; };
58+
59+
// Return the I Thread on the array
60+
// Doesn't perform any bounds checks and behaviour is
61+
// unpredictable in case of index > N
62+
Thread& operator[](int index) { return thread[index]; };
63+
};
64+
65+
#endif

0 commit comments

Comments
 (0)