2 // detail/winapi_thread.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_WINAPI_THREAD_HPP
12 #define BOOST_ASIO_DETAIL_WINAPI_THREAD_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_WINDOWS)
21 #if defined(BOOST_ASIO_WINDOWS_APP) || defined(UNDER_CE)
24 #include <boost/asio/detail/noncopyable.hpp>
25 #include <boost/asio/detail/socket_types.hpp>
26 #include <boost/asio/detail/throw_error.hpp>
27 #include <boost/asio/error.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 DWORD WINAPI winapi_thread_function(LPVOID arg);
42 template <typename Function>
43 winapi_thread(Function f, unsigned int = 0)
45 std::auto_ptr<func_base> arg(new func<Function>(f));
47 thread_ = ::CreateThread(0, 0, winapi_thread_function,
48 arg.get(), 0, &thread_id);
51 DWORD last_error = ::GetLastError();
52 boost::system::error_code ec(last_error,
53 boost::asio::error::get_system_category());
54 boost::asio::detail::throw_error(ec, "thread");
62 ::CloseHandle(thread_);
65 // Wait for the thread to exit.
68 #if defined(BOOST_ASIO_WINDOWS_APP)
69 ::WaitForSingleObjectEx(thread_, INFINITE, false);
70 #else // defined(BOOST_ASIO_WINDOWS_APP)
71 ::WaitForSingleObject(thread_, INFINITE);
72 #endif // defined(BOOST_ASIO_WINDOWS_APP)
76 friend DWORD WINAPI winapi_thread_function(LPVOID arg);
81 virtual ~func_base() {}
82 virtual void run() = 0;
85 template <typename Function>
107 inline DWORD WINAPI winapi_thread_function(LPVOID arg)
109 std::auto_ptr<winapi_thread::func_base> func(
110 static_cast<winapi_thread::func_base*>(arg));
115 } // namespace detail
119 #include <boost/asio/detail/pop_options.hpp>
121 #endif // defined(BOOST_ASIO_WINDOWS_APP) || defined(UNDER_CE)
122 #endif // defined(BOOST_ASIO_WINDOWS)
124 #endif // BOOST_ASIO_DETAIL_WINAPI_THREAD_HPP