wince_thread.hpp 2.5 KB

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