Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

multitasking.hpp 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 State task() = 0;
  10. };
  11. class MultiTaskQueue : public std::queue<MultiTask*> {
  12. public:
  13. void put(MultiTask& T) { push(&T); }
  14. MultiTask& get() { MultiTask& T = *(front()); pop(); return T; }
  15. };
  16. class MultiTasker : public MultiTask {
  17. private:
  18. void processBatch(size_t n, MultiTaskQueue& q, bool StopOnWait = false);
  19. public:
  20. MultiTaskQueue working;
  21. MultiTaskQueue waiting;
  22. MultiTaskQueue finished;
  23. State task();
  24. bool isReady() { return (false == working.empty()); }
  25. bool isWaiting() { return (false == waiting.empty() && true == working.empty()); }
  26. bool isFinished() { return (true == working.empty() && true == waiting.empty()); }
  27. bool hasWork() { return (false == isFinished()); }
  28. size_t completed() { return finished.size(); }
  29. size_t pending() { return (working.size() + waiting.size()); }
  30. size_t total() { return (working.size() + waiting.size() + finished.size()); }
  31. void put(MultiTask& t) { working.put(t); }
  32. MultiTask& get() { return finished.get(); }
  33. bool canGet() { return (false == finished.empty()); }
  34. };
  35. }
  36. #endif // M_MultiTasking