2 // detail/winapp_thread.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_WINAPP_THREAD_HPP
12 #define BOOST_ASIO_DETAIL_WINAPP_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) && defined(BOOST_ASIO_WINDOWS_APP)
22 #include <boost/asio/detail/noncopyable.hpp>
23 #include <boost/asio/detail/scoped_ptr.hpp>
24 #include <boost/asio/detail/socket_types.hpp>
25 #include <boost/asio/detail/throw_error.hpp>
26 #include <boost/asio/error.hpp>
28 #include <boost/asio/detail/push_options.hpp>
34 DWORD WINAPI winapp_thread_function(LPVOID arg);
41 template <typename Function>
42 winapp_thread(Function f, unsigned int = 0)
44 scoped_ptr<func_base> arg(new func<Function>(f));
46 thread_ = ::CreateThread(0, 0, winapp_thread_function,
47 arg.get(), 0, &thread_id);
50 DWORD last_error = ::GetLastError();
51 boost::system::error_code ec(last_error,
52 boost::asio::error::get_system_category());
53 boost::asio::detail::throw_error(ec, "thread");
61 ::CloseHandle(thread_);
64 // Wait for the thread to exit.
67 ::WaitForSingleObjectEx(thread_, INFINITE, false);
70 // Get number of CPUs.
71 static std::size_t hardware_concurrency()
73 SYSTEM_INFO system_info;
74 ::GetNativeSystemInfo(&system_info);
75 return system_info.dwNumberOfProcessors;
79 friend DWORD WINAPI winapp_thread_function(LPVOID arg);
84 virtual ~func_base() {}
85 virtual void run() = 0;
88 template <typename Function>
110 inline DWORD WINAPI winapp_thread_function(LPVOID arg)
112 scoped_ptr<winapp_thread::func_base> func(
113 static_cast<winapp_thread::func_base*>(arg));
118 } // namespace detail
122 #include <boost/asio/detail/pop_options.hpp>
124 #endif // defined(BOOST_ASIO_WINDOWS) && defined(BOOST_ASIO_WINDOWS_APP)
126 #endif // BOOST_ASIO_DETAIL_WINAPP_THREAD_HPP