fix doc example typo
[boost.git] / boost / asio / detail / wince_thread.hpp
blob7b24ec25bf1cca8972c062d81a2212aee2dab98d
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 //
11 #ifndef BOOST_ASIO_DETAIL_WINCE_THREAD_HPP
12 #define BOOST_ASIO_DETAIL_WINCE_THREAD_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/config.hpp>
22 #include <boost/system/system_error.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
25 #if defined(BOOST_WINDOWS) && defined(UNDER_CE)
27 #include <boost/asio/error.hpp>
28 #include <boost/asio/detail/noncopyable.hpp>
29 #include <boost/asio/detail/socket_types.hpp>
31 #include <boost/asio/detail/push_options.hpp>
32 #include <boost/throw_exception.hpp>
33 #include <memory>
34 #include <boost/asio/detail/pop_options.hpp>
36 namespace boost {
37 namespace asio {
38 namespace detail {
40 DWORD WINAPI wince_thread_function(LPVOID arg);
42 class wince_thread
43 : private noncopyable
45 public:
46 // Constructor.
47 template <typename Function>
48 wince_thread(Function f)
50 std::auto_ptr<func_base> arg(new func<Function>(f));
51 DWORD thread_id = 0;
52 thread_ = ::CreateThread(0, 0, wince_thread_function,
53 arg.get(), 0, &thread_id);
54 if (!thread_)
56 DWORD last_error = ::GetLastError();
57 boost::system::system_error e(
58 boost::system::error_code(last_error,
59 boost::asio::error::get_system_category()),
60 "thread");
61 boost::throw_exception(e);
63 arg.release();
66 // Destructor.
67 ~wince_thread()
69 ::CloseHandle(thread_);
72 // Wait for the thread to exit.
73 void join()
75 ::WaitForSingleObject(thread_, INFINITE);
78 private:
79 friend DWORD WINAPI wince_thread_function(LPVOID arg);
81 class func_base
83 public:
84 virtual ~func_base() {}
85 virtual void run() = 0;
88 template <typename Function>
89 class func
90 : public func_base
92 public:
93 func(Function f)
94 : f_(f)
98 virtual void run()
100 f_();
103 private:
104 Function f_;
107 ::HANDLE thread_;
110 inline DWORD WINAPI wince_thread_function(LPVOID arg)
112 std::auto_ptr<wince_thread::func_base> func(
113 static_cast<wince_thread::func_base*>(arg));
114 func->run();
115 return 0;
118 } // namespace detail
119 } // namespace asio
120 } // namespace boost
122 #endif // defined(BOOST_WINDOWS) && defined(UNDER_CE)
124 #include <boost/asio/detail/pop_options.hpp>
126 #endif // BOOST_ASIO_DETAIL_WINCE_THREAD_HPP