std_thread.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // detail/std_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_STD_THREAD_HPP
  11. #define BOOST_ASIO_DETAIL_STD_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. #include <thread>
  17. #include <boost/asio/detail/memory.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace detail {
  22. class std_thread
  23. {
  24. public:
  25. // Construct in a non-joinable state.
  26. std_thread() noexcept
  27. : thread_()
  28. {
  29. }
  30. // Constructor.
  31. template <typename Function>
  32. std_thread(Function f, unsigned int = 0)
  33. : thread_(f)
  34. {
  35. }
  36. // Construct with custom allocator.
  37. template <typename Allocator, typename Function>
  38. std_thread(allocator_arg_t, const Allocator&, Function f, unsigned int = 0)
  39. : thread_(f)
  40. {
  41. }
  42. // Move constructor.
  43. std_thread(std_thread&& other) noexcept
  44. : thread_(static_cast<std::thread&&>(other.thread_))
  45. {
  46. }
  47. // Destructor.
  48. ~std_thread()
  49. {
  50. }
  51. // Move assignment.
  52. std_thread& operator=(std_thread&& other) noexcept
  53. {
  54. thread_ = static_cast<std::thread&&>(other.thread_);
  55. return *this;
  56. }
  57. // Whether the thread can be joined.
  58. bool joinable() const
  59. {
  60. return thread_.joinable();
  61. }
  62. // Wait for the thread to exit.
  63. void join()
  64. {
  65. if (thread_.joinable())
  66. thread_.join();
  67. }
  68. // Get number of CPUs.
  69. static std::size_t hardware_concurrency()
  70. {
  71. return std::thread::hardware_concurrency();
  72. }
  73. private:
  74. std::thread thread_;
  75. };
  76. } // namespace detail
  77. } // namespace asio
  78. } // namespace boost
  79. #include <boost/asio/detail/pop_options.hpp>
  80. #endif // BOOST_ASIO_DETAIL_STD_THREAD_HPP