2
0
mirror of https://github.com/boostorg/asio.git synced 2026-02-26 02:42:08 +00:00

Only use TerminateThread when explicitly requested by the user by calling

asio::detail::thread::set_terminate_threads(true). This fixes a memory leak
that may occur with internally created threads.


[SVN r49201]
This commit is contained in:
Christopher Kohlhoff
2008-10-09 06:33:34 +00:00
parent 6e64678759
commit b06aeaea60
4 changed files with 78 additions and 54 deletions

View File

@@ -39,12 +39,9 @@ class null_thread
: private noncopyable
{
public:
// The purpose of the thread.
enum purpose { internal, external };
// Constructor.
template <typename Function>
null_thread(Function f, purpose = internal)
null_thread(Function f)
{
boost::system::system_error e(
boost::asio::error::operation_not_supported, "thread");

View File

@@ -43,12 +43,9 @@ class posix_thread
: private noncopyable
{
public:
// The purpose of the thread.
enum purpose { internal, external };
// Constructor.
template <typename Function>
posix_thread(Function f, purpose = internal)
posix_thread(Function f)
: joined_(false)
{
std::auto_ptr<func_base> arg(new func<Function>(f));

View File

@@ -40,50 +40,67 @@ namespace detail {
unsigned int __stdcall win_thread_function(void* arg);
class win_thread
: private noncopyable
#if (WINVER < 0x0500)
void __stdcall apc_function(ULONG data);
#else
void __stdcall apc_function(ULONG_PTR data);
#endif
template <typename T>
class win_thread_base
{
public:
// The purpose of the thread.
enum purpose { internal, external };
static bool terminate_threads()
{
return ::InterlockedExchangeAdd(&terminate_threads_, 0) != 0;
}
static void set_terminate_threads(bool b)
{
::InterlockedExchange(&terminate_threads_, b ? 1 : 0);
}
private:
static long terminate_threads_;
};
template <typename T>
long win_thread_base<T>::terminate_threads_ = 0;
class win_thread
: private noncopyable,
public win_thread_base<win_thread>
{
public:
// Constructor.
template <typename Function>
win_thread(Function f, purpose p = internal)
win_thread(Function f)
: exit_event_(0)
{
std::auto_ptr<func_base> arg(new func<Function>(f));
::HANDLE entry_event = 0;
if (p == internal)
arg->entry_event_ = entry_event = ::CreateEvent(0, true, false, 0);
if (!entry_event)
{
arg->entry_event_ = entry_event = ::CreateEvent(0, true, false, 0);
if (!entry_event)
{
DWORD last_error = ::GetLastError();
boost::system::system_error e(
boost::system::error_code(last_error,
boost::asio::error::get_system_category()),
"thread.entry_event");
boost::throw_exception(e);
}
arg->exit_event_ = exit_event_ = ::CreateEvent(0, true, false, 0);
if (!exit_event_)
{
DWORD last_error = ::GetLastError();
::CloseHandle(entry_event);
boost::system::system_error e(
boost::system::error_code(last_error,
boost::asio::error::get_system_category()),
"thread.exit_event");
boost::throw_exception(e);
}
DWORD last_error = ::GetLastError();
boost::system::system_error e(
boost::system::error_code(last_error,
boost::asio::error::get_system_category()),
"thread.entry_event");
boost::throw_exception(e);
}
else
arg->exit_event_ = exit_event_ = ::CreateEvent(0, true, false, 0);
if (!exit_event_)
{
arg->entry_event_ = 0;
arg->exit_event_ = 0;
DWORD last_error = ::GetLastError();
::CloseHandle(entry_event);
boost::system::system_error e(
boost::system::error_code(last_error,
boost::asio::error::get_system_category()),
"thread.exit_event");
boost::throw_exception(e);
}
unsigned int thread_id = 0;
@@ -123,14 +140,15 @@ public:
// Wait for the thread to exit.
void join()
{
if (exit_event_)
::WaitForSingleObject(exit_event_, INFINITE);
::CloseHandle(exit_event_);
if (terminate_threads())
{
::WaitForSingleObject(exit_event_, INFINITE);
::CloseHandle(exit_event_);
::TerminateThread(thread_, 0);
}
else
{
::QueueUserAPC(apc_function, thread_, 0);
::WaitForSingleObject(thread_, INFINITE);
}
}
@@ -138,6 +156,12 @@ public:
private:
friend unsigned int __stdcall win_thread_function(void* arg);
#if (WINVER < 0x0500)
friend void __stdcall apc_function(ULONG);
#else
friend void __stdcall apc_function(ULONG_PTR);
#endif
class func_base
{
public:
@@ -175,21 +199,30 @@ inline unsigned int __stdcall win_thread_function(void* arg)
std::auto_ptr<win_thread::func_base> func(
static_cast<win_thread::func_base*>(arg));
if (func->entry_event_)
::SetEvent(func->entry_event_);
::SetEvent(func->entry_event_);
func->run();
if (HANDLE exit_event = func->exit_event_)
{
func.reset();
::SetEvent(exit_event);
::Sleep(INFINITE);
}
// Signal that the thread has finished its work, but rather than returning go
// to sleep to put the thread into a well known state. If the thread is being
// joined during global object destruction then it may be killed using
// TerminateThread (to avoid a deadlock in DllMain). Otherwise, the SleepEx
// call will be interrupted using QueueUserAPC and the thread will shut down
// cleanly.
HANDLE exit_event = func->exit_event_;
func.reset();
::SetEvent(exit_event);
::SleepEx(INFINITE, TRUE);
return 0;
}
#if (WINVER < 0x0500)
inline void __stdcall apc_function(ULONG) {}
#else
inline void __stdcall apc_function(ULONG_PTR) {}
#endif
} // namespace detail
} // namespace asio
} // namespace boost

View File

@@ -43,12 +43,9 @@ class wince_thread
: private noncopyable
{
public:
// The purpose of the thread.
enum purpose { internal, external };
// Constructor.
template <typename Function>
wince_thread(Function f, purpose = internal)
wince_thread(Function f)
{
std::auto_ptr<func_base> arg(new func<Function>(f));
DWORD thread_id = 0;