winapp_thread.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // detail/winapp_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_WINAPP_THREAD_HPP
  11. #define BOOST_ASIO_DETAIL_WINAPP_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_WINDOWS) && defined(BOOST_ASIO_WINDOWS_APP)
  17. #include <boost/asio/detail/noncopyable.hpp>
  18. #include <boost/asio/detail/scoped_ptr.hpp>
  19. #include <boost/asio/detail/socket_types.hpp>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. DWORD WINAPI winapp_thread_function(LPVOID arg);
  27. class winapp_thread
  28. : private noncopyable
  29. {
  30. public:
  31. // Constructor.
  32. template <typename Function>
  33. winapp_thread(Function f, unsigned int = 0)
  34. {
  35. scoped_ptr<func_base> arg(new func<Function>(f));
  36. DWORD thread_id = 0;
  37. thread_ = ::CreateThread(0, 0, winapp_thread_function,
  38. arg.get(), 0, &thread_id);
  39. if (!thread_)
  40. {
  41. DWORD last_error = ::GetLastError();
  42. boost::system::error_code ec(last_error,
  43. boost::asio::error::get_system_category());
  44. boost::asio::detail::throw_error(ec, "thread");
  45. }
  46. arg.release();
  47. }
  48. // Destructor.
  49. ~winapp_thread()
  50. {
  51. ::CloseHandle(thread_);
  52. }
  53. // Wait for the thread to exit.
  54. void join()
  55. {
  56. ::WaitForSingleObjectEx(thread_, INFINITE, false);
  57. }
  58. // Get number of CPUs.
  59. static std::size_t hardware_concurrency()
  60. {
  61. SYSTEM_INFO system_info;
  62. ::GetNativeSystemInfo(&system_info);
  63. return system_info.dwNumberOfProcessors;
  64. }
  65. private:
  66. friend DWORD WINAPI winapp_thread_function(LPVOID arg);
  67. class func_base
  68. {
  69. public:
  70. virtual ~func_base() {}
  71. virtual void run() = 0;
  72. };
  73. template <typename Function>
  74. class func
  75. : public func_base
  76. {
  77. public:
  78. func(Function f)
  79. : f_(f)
  80. {
  81. }
  82. virtual void run()
  83. {
  84. f_();
  85. }
  86. private:
  87. Function f_;
  88. };
  89. ::HANDLE thread_;
  90. };
  91. inline DWORD WINAPI winapp_thread_function(LPVOID arg)
  92. {
  93. scoped_ptr<winapp_thread::func_base> func(
  94. static_cast<winapp_thread::func_base*>(arg));
  95. func->run();
  96. return 0;
  97. }
  98. } // namespace detail
  99. } // namespace asio
  100. } // namespace boost
  101. #include <boost/asio/detail/pop_options.hpp>
  102. #endif // defined(BOOST_ASIO_WINDOWS) && defined(BOOST_ASIO_WINDOWS_APP)
  103. #endif // BOOST_ASIO_DETAIL_WINAPP_THREAD_HPP