2 // detail/impl/win_thread.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2014 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_IMPL_WIN_THREAD_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP
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(UNDER_CE)
23 #include <boost/asio/detail/throw_error.hpp>
24 #include <boost/asio/detail/win_thread.hpp>
25 #include <boost/asio/error.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 win_thread::~win_thread()
35 ::CloseHandle(thread_);
37 // The exit_event_ handle is deliberately allowed to leak here since it
38 // is an error for the owner of an internal thread not to join() it.
41 void win_thread::join()
43 HANDLE handles[2] = { exit_event_, thread_ };
44 ::WaitForMultipleObjects(2, handles, FALSE, INFINITE);
45 ::CloseHandle(exit_event_);
46 if (terminate_threads())
48 ::TerminateThread(thread_, 0);
52 ::QueueUserAPC(apc_function, thread_, 0);
53 ::WaitForSingleObject(thread_, INFINITE);
57 void win_thread::start_thread(func_base* arg, unsigned int stack_size)
59 ::HANDLE entry_event = 0;
60 arg->entry_event_ = entry_event = ::CreateEvent(0, true, false, 0);
63 DWORD last_error = ::GetLastError();
65 boost::system::error_code ec(last_error,
66 boost::asio::error::get_system_category());
67 boost::asio::detail::throw_error(ec, "thread.entry_event");
70 arg->exit_event_ = exit_event_ = ::CreateEvent(0, true, false, 0);
73 DWORD last_error = ::GetLastError();
75 boost::system::error_code ec(last_error,
76 boost::asio::error::get_system_category());
77 boost::asio::detail::throw_error(ec, "thread.exit_event");
80 unsigned int thread_id = 0;
81 thread_ = reinterpret_cast<HANDLE>(::_beginthreadex(0,
82 stack_size, win_thread_function, arg, 0, &thread_id));
85 DWORD last_error = ::GetLastError();
88 ::CloseHandle(entry_event);
90 ::CloseHandle(exit_event_);
91 boost::system::error_code ec(last_error,
92 boost::asio::error::get_system_category());
93 boost::asio::detail::throw_error(ec, "thread");
98 ::WaitForSingleObject(entry_event, INFINITE);
99 ::CloseHandle(entry_event);
103 unsigned int __stdcall win_thread_function(void* arg)
105 win_thread::auto_func_base_ptr func = {
106 static_cast<win_thread::func_base*>(arg) };
108 ::SetEvent(func.ptr->entry_event_);
112 // Signal that the thread has finished its work, but rather than returning go
113 // to sleep to put the thread into a well known state. If the thread is being
114 // joined during global object destruction then it may be killed using
115 // TerminateThread (to avoid a deadlock in DllMain). Otherwise, the SleepEx
116 // call will be interrupted using QueueUserAPC and the thread will shut down
118 HANDLE exit_event = func.ptr->exit_event_;
121 ::SetEvent(exit_event);
122 ::SleepEx(INFINITE, TRUE);
127 #if defined(WINVER) && (WINVER < 0x0500)
128 void __stdcall apc_function(ULONG) {}
130 void __stdcall apc_function(ULONG_PTR) {}
133 } // namespace detail
137 #include <boost/asio/detail/pop_options.hpp>
139 #endif // defined(BOOST_ASIO_WINDOWS) && !defined(UNDER_CE)
141 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_THREAD_IPP