task_queue.hpp 609 B

123456789101112131415161718192021222324252627282930
  1. #include <atomic>
  2. #include <condition_variable>
  3. #include <functional>
  4. #include <mutex>
  5. #include <queue>
  6. #include <thread>
  7. class TaskQueue {
  8. public:
  9. TaskQueue();
  10. void initialize();
  11. void notifyPool();
  12. void pushToQueue(std::function<void()> fn);
  13. private:
  14. void handleLoop();
  15. void tasksReady();
  16. void done();
  17. void detachThreads();
  18. std::queue<std::function<void()>> m_task_queue;
  19. // ThreadPool m_thread_pool;
  20. std::vector<std::thread> m_thread_pool;
  21. std::thread m_loop_thread;
  22. std::mutex m_mutex_lock;
  23. std::condition_variable pool_condition;
  24. std::atomic<bool> accepting_tasks;
  25. };