std_thread.hpp 1.2 KB

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