Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

multitasking.hpp 1.3KB

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