std_thread.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // detail/std_thread.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. #if defined(BOOST_ASIO_HAS_STD_THREAD)
  17. #include <thread>
  18. #include <boost/asio/detail/noncopyable.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. class std_thread
  24. : private noncopyable
  25. {
  26. public:
  27. // Constructor.
  28. template <typename Function>
  29. std_thread(Function f, unsigned int = 0)
  30. : thread_(f)
  31. {
  32. }
  33. // Destructor.
  34. ~std_thread()
  35. {
  36. join();
  37. }
  38. // Wait for the thread to exit.
  39. void join()
  40. {
  41. if (thread_.joinable())
  42. thread_.join();
  43. }
  44. // Get number of CPUs.
  45. static std::size_t hardware_concurrency()
  46. {
  47. return std::thread::hardware_concurrency();
  48. }
  49. private:
  50. std::thread thread_;
  51. };
  52. } // namespace detail
  53. } // namespace asio
  54. } // namespace boost
  55. #include <boost/asio/detail/pop_options.hpp>
  56. #endif // defined(BOOST_ASIO_HAS_STD_THREAD)
  57. #endif // BOOST_ASIO_DETAIL_STD_THREAD_HPP