Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

69 righe
1.8 KiB

  1. #pragma once
  2. #include <atomic>
  3. #include <vector>
  4. #include <ecs/config.h>
  5. #include <ecs/core/utils/thread_pool/worker.h>
  6. #include <ecs/core/utils/thread_pool/task_queue.h>
  7. namespace ecs {
  8. namespace core {
  9. namespace utils {
  10. /**
  11. * this class implements a thread pool
  12. */
  13. struct thread_pool
  14. {
  15. private:
  16. using worker_ptr_u = std::unique_ptr<thread_pool_worker>;
  17. using worker_vector = std::vector<worker_ptr_u>;
  18. using atomic_size_t = std::atomic<size_t>;
  19. private:
  20. concurrent_task_queue _tasks; //!< queue to store tasks to execute
  21. worker_vector _workers; //!< vector of worker threads
  22. atomic_size_t _outstanding_inits; //!< number of outstanding worker initializations
  23. /**
  24. * check if all workers are finished or not
  25. *
  26. * @retval TRUE if all workers are finished
  27. * @retval FALSE if at least one worker is running
  28. */
  29. auto all_workers_finished() const noexcept;
  30. /**
  31. * initialize worker threads
  32. *
  33. * @param count number of workers to create
  34. */
  35. void initialize_workers(size_t count);
  36. public:
  37. /**
  38. * constructor
  39. *
  40. * @param count number of workers to create
  41. */
  42. thread_pool(size_t count);
  43. /**
  44. * destructor
  45. */
  46. ~thread_pool();
  47. /**
  48. * enqueue a new task inside the thread pool
  49. *
  50. * @tparam T_task type of the task
  51. *
  52. * @param task task to execute
  53. * @param worker_id id of the worker to enqueue task at (-1 = any; n = worker tasks)
  54. */
  55. template<typename T_task>
  56. inline void post(T_task&& task, ssize_t worker_id = -1);
  57. };
  58. } } }