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.

thread_group.hpp 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // detail/thread_group.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_DETAIL_THREAD_GROUP_HPP
  11. #define ASIO_DETAIL_THREAD_GROUP_HPP
  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/detail/scoped_ptr.hpp"
  17. #include "asio/detail/thread.hpp"
  18. namespace asio {
  19. namespace detail {
  20. class thread_group
  21. {
  22. public:
  23. // Constructor initialises an empty thread group.
  24. thread_group()
  25. : first_(0)
  26. {
  27. }
  28. // Destructor joins any remaining threads in the group.
  29. ~thread_group()
  30. {
  31. join();
  32. }
  33. // Create a new thread in the group.
  34. template <typename Function>
  35. void create_thread(Function f)
  36. {
  37. first_ = new item(f, first_);
  38. }
  39. // Create new threads in the group.
  40. template <typename Function>
  41. void create_threads(Function f, std::size_t num_threads)
  42. {
  43. for (std::size_t i = 0; i < num_threads; ++i)
  44. create_thread(f);
  45. }
  46. // Wait for all threads in the group to exit.
  47. void join()
  48. {
  49. while (first_)
  50. {
  51. first_->thread_.join();
  52. item* tmp = first_;
  53. first_ = first_->next_;
  54. delete tmp;
  55. }
  56. }
  57. private:
  58. // Structure used to track a single thread in the group.
  59. struct item
  60. {
  61. template <typename Function>
  62. explicit item(Function f, item* next)
  63. : thread_(f),
  64. next_(next)
  65. {
  66. }
  67. asio::detail::thread thread_;
  68. item* next_;
  69. };
  70. // The first thread in the group.
  71. item* first_;
  72. };
  73. } // namespace detail
  74. } // namespace asio
  75. #endif // ASIO_DETAIL_THREAD_GROUP_HPP