1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- #ifndef M_MultiTasking
- #define M_MultiTasking
-
- #include <cstddef>
- #include <queue>
-
- namespace CodeDweller {
-
- class MultiTask {
- public:
- enum class State{Running, Waiting, Finished};
- virtual ~MultiTask() {}
- virtual State task() = 0;
- };
-
- class MultiTaskQueue : public std::queue<MultiTask*> {
- public:
-
- void put(MultiTask& T) { push(&T); }
- MultiTask& get() { MultiTask& T = *(front()); pop(); return T; }
-
- };
-
- class MultiTasker : public MultiTask {
- private:
- void processBatch(size_t n, MultiTaskQueue& q, bool StopOnWait = false);
-
- public:
- MultiTaskQueue working;
- MultiTaskQueue waiting;
- MultiTaskQueue finished;
- State task();
-
- bool isReady() { return (false == working.empty()); }
- bool isWaiting() { return (false == waiting.empty() && true == working.empty()); }
- bool isFinished() { return (true == working.empty() && true == waiting.empty()); }
- bool hasWork() { return (false == isFinished()); }
-
- size_t completed() { return finished.size(); }
- size_t pending() { return (working.size() + waiting.size()); }
- size_t total() { return (working.size() + waiting.size() + finished.size()); }
-
- void put(MultiTask& t) { working.put(t); }
- MultiTask& get() { return finished.get(); }
- bool canGet() { return (false == finished.empty()); }
- };
-
- }
-
- #endif // M_MultiTasking
|