2 // detail/win_thread.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 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_WIN_THREAD_HPP
12 #define BOOST_ASIO_DETAIL_WIN_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 && !defined(BOOST_ASIO_WINDOWS_APP) \
25 #include <boost/asio/detail/noncopyable.hpp>
26 #include <boost/asio/detail/socket_types.hpp>
28 #include <boost/asio/detail/push_options.hpp>
34 BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
36 #if defined(WINVER) && (WINVER < 0x0500)
37 BOOST_ASIO_DECL void __stdcall apc_function(ULONG data);
39 BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR data);
46 static bool terminate_threads()
48 return ::InterlockedExchangeAdd(&terminate_threads_, 0) != 0;
51 static void set_terminate_threads(bool b)
53 ::InterlockedExchange(&terminate_threads_, b ? 1 : 0);
57 static long terminate_threads_;
61 long win_thread_base<T>::terminate_threads_ = 0;
64 : private noncopyable,
65 public win_thread_base<win_thread>
69 template <typename Function>
70 win_thread(Function f, unsigned int stack_size = 0)
74 start_thread(new func<Function>(f), stack_size);
78 BOOST_ASIO_DECL ~win_thread();
80 // Wait for the thread to exit.
81 BOOST_ASIO_DECL void join();
83 // Get number of CPUs.
84 BOOST_ASIO_DECL static std::size_t hardware_concurrency();
87 friend BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
89 #if defined(WINVER) && (WINVER < 0x0500)
90 friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG);
92 friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR);
98 virtual ~func_base() {}
99 virtual void run() = 0;
100 ::HANDLE entry_event_;
101 ::HANDLE exit_event_;
104 struct auto_func_base_ptr
107 ~auto_func_base_ptr() { delete ptr; }
110 template <typename Function>
129 BOOST_ASIO_DECL void start_thread(func_base* arg, unsigned int stack_size);
132 ::HANDLE exit_event_;
135 } // namespace detail
139 #include <boost/asio/detail/pop_options.hpp>
141 #if defined(BOOST_ASIO_HEADER_ONLY)
142 # include <boost/asio/detail/impl/win_thread.ipp>
143 #endif // defined(BOOST_ASIO_HEADER_ONLY)
145 #endif // defined(BOOST_ASIO_WINDOWS)
146 // && !defined(BOOST_ASIO_WINDOWS_APP)
147 // && !defined(UNDER_CE)
149 #endif // BOOST_ASIO_DETAIL_WIN_THREAD_HPP