2 // detail/win_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_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) \
24 #include <boost/asio/detail/noncopyable.hpp>
25 #include <boost/asio/detail/socket_types.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
35 #if defined(WINVER) && (WINVER < 0x0500)
36 BOOST_ASIO_DECL void __stdcall apc_function(ULONG data);
38 BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR data);
45 static bool terminate_threads()
47 return ::InterlockedExchangeAdd(&terminate_threads_, 0) != 0;
50 static void set_terminate_threads(bool b)
52 ::InterlockedExchange(&terminate_threads_, b ? 1 : 0);
56 static long terminate_threads_;
60 long win_thread_base<T>::terminate_threads_ = 0;
63 : private noncopyable,
64 public win_thread_base<win_thread>
68 template <typename Function>
69 win_thread(Function f, unsigned int stack_size = 0)
73 start_thread(new func<Function>(f), stack_size);
77 BOOST_ASIO_DECL ~win_thread();
79 // Wait for the thread to exit.
80 BOOST_ASIO_DECL void join();
83 friend BOOST_ASIO_DECL unsigned int __stdcall win_thread_function(void* arg);
85 #if defined(WINVER) && (WINVER < 0x0500)
86 friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG);
88 friend BOOST_ASIO_DECL void __stdcall apc_function(ULONG_PTR);
94 virtual ~func_base() {}
95 virtual void run() = 0;
96 ::HANDLE entry_event_;
100 struct auto_func_base_ptr
103 ~auto_func_base_ptr() { delete ptr; }
106 template <typename Function>
125 BOOST_ASIO_DECL void start_thread(func_base* arg, unsigned int stack_size);
128 ::HANDLE exit_event_;
131 } // namespace detail
135 #include <boost/asio/detail/pop_options.hpp>
137 #if defined(BOOST_ASIO_HEADER_ONLY)
138 # include <boost/asio/detail/impl/win_thread.ipp>
139 #endif // defined(BOOST_ASIO_HEADER_ONLY)
141 #endif // defined(BOOST_ASIO_WINDOWS)
142 // && !defined(BOOST_ASIO_WINDOWS_APP)
143 // && !defined(UNDER_CE)
145 #endif // BOOST_ASIO_DETAIL_WIN_THREAD_HPP