wince_thread.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // detail/wince_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_WINCE_THREAD_HPP
  11. #define BOOST_ASIO_DETAIL_WINCE_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(UNDER_CE)
  17. #include <memory>
  18. #include <boost/asio/detail/noncopyable.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 wince_thread_function(LPVOID arg);
  27. class wince_thread
  28. : private noncopyable
  29. {
  30. public:
  31. // Constructor.
  32. template <typename Function>
  33. wince_thread(Function f, unsigned int = 0)
  34. {
  35. std::auto_ptr<func_base> arg(new func<Function>(f));
  36. DWORD thread_id = 0;
  37. thread_ = ::CreateThread(0, 0, wince_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. ~wince_thread()
  50. {
  51. ::CloseHandle(thread_);
  52. }
  53. // Wait for the thread to exit.
  54. void join()
  55. {
  56. ::WaitForSingleObject(thread_, INFINITE);
  57. }
  58. private:
  59. friend DWORD WINAPI wince_thread_function(LPVOID arg);
  60. class func_base
  61. {
  62. public:
  63. virtual ~func_base() {}
  64. virtual void run() = 0;
  65. };
  66. template <typename Function>
  67. class func
  68. : public func_base
  69. {
  70. public:
  71. func(Function f)
  72. : f_(f)
  73. {
  74. }
  75. virtual void run()
  76. {
  77. f_();
  78. }
  79. private:
  80. Function f_;
  81. };
  82. ::HANDLE thread_;
  83. };
  84. inline DWORD WINAPI wince_thread_function(LPVOID arg)
  85. {
  86. std::auto_ptr<wince_thread::func_base> func(
  87. static_cast<wince_thread::func_base*>(arg));
  88. func->run();
  89. return 0;
  90. }
  91. } // namespace detail
  92. } // namespace asio
  93. } // namespace boost
  94. #include <boost/asio/detail/pop_options.hpp>
  95. #endif // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  96. #endif // BOOST_ASIO_DETAIL_WINCE_THREAD_HPP