Audio plugin host https://kx.studio/carla
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.

88 lines
1.8KB

  1. //
  2. // impl/thread_pool.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_IMPL_THREAD_POOL_IPP
  11. #define ASIO_IMPL_THREAD_POOL_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/thread_pool.hpp"
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. struct thread_pool::thread_function
  20. {
  21. detail::scheduler* scheduler_;
  22. void operator()()
  23. {
  24. asio::error_code ec;
  25. scheduler_->run(ec);
  26. }
  27. };
  28. thread_pool::thread_pool()
  29. : scheduler_(add_scheduler(new detail::scheduler(*this, 0, false)))
  30. {
  31. scheduler_.work_started();
  32. thread_function f = { &scheduler_ };
  33. std::size_t num_threads = detail::thread::hardware_concurrency() * 2;
  34. threads_.create_threads(f, num_threads ? num_threads : 2);
  35. }
  36. thread_pool::thread_pool(std::size_t num_threads)
  37. : scheduler_(add_scheduler(new detail::scheduler(
  38. *this, num_threads == 1 ? 1 : 0, false)))
  39. {
  40. scheduler_.work_started();
  41. thread_function f = { &scheduler_ };
  42. threads_.create_threads(f, num_threads);
  43. }
  44. thread_pool::~thread_pool()
  45. {
  46. stop();
  47. join();
  48. }
  49. void thread_pool::stop()
  50. {
  51. scheduler_.stop();
  52. }
  53. void thread_pool::join()
  54. {
  55. if (!threads_.empty())
  56. {
  57. scheduler_.work_finished();
  58. threads_.join();
  59. }
  60. }
  61. detail::scheduler& thread_pool::add_scheduler(detail::scheduler* s)
  62. {
  63. detail::scoped_ptr<detail::scheduler> scoped_impl(s);
  64. asio::add_service<detail::scheduler>(*this, scoped_impl.get());
  65. return *scoped_impl.release();
  66. }
  67. } // namespace asio
  68. #include "asio/detail/pop_options.hpp"
  69. #endif // ASIO_IMPL_THREAD_POOL_IPP