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.

59 line
1.1 KiB

  1. #include <ecs/core/utils/fixed_function.inl>
  2. #include <ecs/core/utils/thread_pool/pool.inl>
  3. #include <ecs/core/utils/thread_pool/worker.inl>
  4. using namespace ::ecs::core::utils;
  5. auto thread_pool
  6. ::all_workers_finished() const noexcept
  7. {
  8. for (const auto& w : _workers)
  9. {
  10. if (!w->finished())
  11. {
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. void thread_pool
  18. ::initialize_workers(size_t count)
  19. {
  20. _workers.reserve(count);
  21. for (size_t i = 0; i < count; ++i)
  22. {
  23. _workers.emplace_back(std::make_unique<thread_pool_worker>(_tasks, i + 1));
  24. }
  25. _outstanding_inits = count;
  26. for (auto& w : _workers)
  27. {
  28. w->start(_outstanding_inits);
  29. }
  30. }
  31. thread_pool
  32. ::thread_pool(size_t count)
  33. {
  34. assert(count > 1);
  35. initialize_workers(count);
  36. }
  37. thread_pool
  38. ::~thread_pool()
  39. {
  40. // wait for uninitialized workers
  41. while (_outstanding_inits > 0)
  42. {
  43. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  44. }
  45. // join the worker threads
  46. for (auto& w : _workers)
  47. {
  48. w->stop();
  49. w->signal();
  50. w->join();
  51. }
  52. }