posix_thread.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // detail/posix_thread.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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 BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
  11. #define BOOST_ASIO_DETAIL_POSIX_THREAD_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_PTHREADS)
  17. #include <cstddef>
  18. #include <pthread.h>
  19. #include <boost/asio/detail/memory.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. extern "C"
  25. {
  26. BOOST_ASIO_DECL void* boost_asio_detail_posix_thread_function(void* arg);
  27. }
  28. class posix_thread
  29. {
  30. public:
  31. // Construct in a non-joinable state.
  32. posix_thread() noexcept
  33. : arg_(0)
  34. {
  35. }
  36. // Constructor.
  37. template <typename Function>
  38. posix_thread(Function f, unsigned int = 0)
  39. : posix_thread(std::allocator_arg, std::allocator<void>(), f)
  40. {
  41. }
  42. // Construct with custom allocator.
  43. template <typename Allocator, typename Function>
  44. posix_thread(allocator_arg_t, const Allocator& a,
  45. Function f, unsigned int = 0)
  46. : arg_(start_thread(allocate_object<func<Function, Allocator>>(a, f, a)))
  47. {
  48. }
  49. // Move constructor.
  50. posix_thread(posix_thread&& other) noexcept
  51. : arg_(other.arg_)
  52. {
  53. other.arg_ = 0;
  54. }
  55. // Destructor.
  56. BOOST_ASIO_DECL ~posix_thread();
  57. // Move assignment.
  58. posix_thread& operator=(posix_thread&& other) noexcept
  59. {
  60. arg_ = other.arg_;
  61. other.arg_ = 0;
  62. return *this;
  63. }
  64. // Whether the thread can be joined.
  65. bool joinable() const
  66. {
  67. return !!arg_;
  68. }
  69. // Wait for the thread to exit.
  70. BOOST_ASIO_DECL void join();
  71. // Get number of CPUs.
  72. BOOST_ASIO_DECL static std::size_t hardware_concurrency();
  73. private:
  74. friend void* boost_asio_detail_posix_thread_function(void* arg);
  75. class func_base
  76. {
  77. public:
  78. virtual ~func_base() {}
  79. virtual void run() = 0;
  80. virtual void destroy() = 0;
  81. ::pthread_t thread_;
  82. };
  83. template <typename Function, typename Allocator>
  84. class func
  85. : public func_base
  86. {
  87. public:
  88. func(Function f, const Allocator& a)
  89. : f_(f),
  90. allocator_(a)
  91. {
  92. }
  93. virtual void run()
  94. {
  95. f_();
  96. }
  97. virtual void destroy()
  98. {
  99. deallocate_object(allocator_, this);
  100. }
  101. private:
  102. Function f_;
  103. Allocator allocator_;
  104. };
  105. BOOST_ASIO_DECL func_base* start_thread(func_base* arg);
  106. func_base* arg_;
  107. };
  108. } // namespace detail
  109. } // namespace asio
  110. } // namespace boost
  111. #include <boost/asio/detail/pop_options.hpp>
  112. #if defined(BOOST_ASIO_HEADER_ONLY)
  113. # include <boost/asio/detail/impl/posix_thread.ipp>
  114. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  115. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  116. #endif // BOOST_ASIO_DETAIL_POSIX_THREAD_HPP