winapi_thread.hpp 2.7 KB

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