wince_thread.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // wince_thread.hpp
  3. // ~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/config.hpp>
  18. #include <boost/system/system_error.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #if defined(BOOST_WINDOWS) && defined(UNDER_CE)
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/detail/noncopyable.hpp>
  23. #include <boost/asio/detail/socket_types.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. #include <boost/throw_exception.hpp>
  26. #include <memory>
  27. #include <boost/asio/detail/pop_options.hpp>
  28. namespace boost {
  29. namespace asio {
  30. namespace detail {
  31. DWORD WINAPI wince_thread_function(LPVOID arg);
  32. class wince_thread
  33. : private noncopyable
  34. {
  35. public:
  36. // Constructor.
  37. template <typename Function>
  38. wince_thread(Function f)
  39. {
  40. std::auto_ptr<func_base> arg(new func<Function>(f));
  41. DWORD thread_id = 0;
  42. thread_ = ::CreateThread(0, 0, wince_thread_function,
  43. arg.get(), 0, &thread_id);
  44. if (!thread_)
  45. {
  46. DWORD last_error = ::GetLastError();
  47. boost::system::system_error e(
  48. boost::system::error_code(last_error,
  49. boost::asio::error::get_system_category()),
  50. "thread");
  51. boost::throw_exception(e);
  52. }
  53. arg.release();
  54. }
  55. // Destructor.
  56. ~wince_thread()
  57. {
  58. ::CloseHandle(thread_);
  59. }
  60. // Wait for the thread to exit.
  61. void join()
  62. {
  63. ::WaitForSingleObject(thread_, INFINITE);
  64. }
  65. private:
  66. friend DWORD WINAPI wince_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 wince_thread_function(LPVOID arg)
  92. {
  93. std::auto_ptr<wince_thread::func_base> func(
  94. static_cast<wince_thread::func_base*>(arg));
  95. func->run();
  96. return 0;
  97. }
  98. } // namespace detail
  99. } // namespace asio
  100. } // namespace boost
  101. #endif // defined(BOOST_WINDOWS) && defined(UNDER_CE)
  102. #include <boost/asio/detail/pop_options.hpp>
  103. #endif // BOOST_ASIO_DETAIL_WINCE_THREAD_HPP