You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

multitasking.hpp 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef M_MultiTasking
  2. #define M_MultiTasking
  3. #include <cstddef>
  4. #include <queue>
  5. namespace CodeDweller {
  6. class MultiTask {
  7. public:
  8. enum class State{Running, Waiting, Finished};
  9. virtual ~MultiTask() {}
  10. virtual State task() = 0;
  11. };
  12. class MultiTaskQueue : public std::queue<MultiTask*> {
  13. public:
  14. void put(MultiTask& T) { push(&T); }
  15. MultiTask& get() { MultiTask& T = *(front()); pop(); return T; }
  16. };
  17. class MultiTasker : public MultiTask {
  18. private:
  19. void processBatch(size_t n, MultiTaskQueue& q, bool StopOnWait = false);
  20. public:
  21. MultiTaskQueue working;
  22. MultiTaskQueue waiting;
  23. MultiTaskQueue finished;
  24. State task();
  25. bool isReady() { return (false == working.empty()); }
  26. bool isWaiting() { return (false == waiting.empty() && true == working.empty()); }
  27. bool isFinished() { return (true == working.empty() && true == waiting.empty()); }
  28. bool hasWork() { return (false == isFinished()); }
  29. size_t completed() { return finished.size(); }
  30. size_t pending() { return (working.size() + waiting.size()); }
  31. size_t total() { return (working.size() + waiting.size() + finished.size()); }
  32. void put(MultiTask& t) { working.put(t); }
  33. MultiTask& get() { return finished.get(); }
  34. bool canGet() { return (false == finished.empty()); }
  35. };
  36. }
  37. #endif // M_MultiTasking