Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
When the C++ REST SDK is built as a DLL on Windows, and unloaded befo…
…re the process exits, the crossplat::threadpool::shared_instance() is destroyed at DLL_PROCESS_DETACH, at which stage joining threads causes deadlock. Use the workaround provided by asio to terminate the threads in this case.
  • Loading branch information
garethsb committed Nov 24, 2017
commit f250c4ca3395759780892a7c76cb209298fe34ff
39 changes: 39 additions & 0 deletions Release/src/pplx/threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ struct threadpool_impl final : crossplat::threadpool
pthread_t t = *iter;
void* res;
pthread_join(t, &res);
#elif defined (_WIN32) && defined(_WINDLL)
if (boost::asio::detail::win_thread::terminate_threads())
{
::TerminateThread(iter->native_handle(), 0);
iter->detach();
}
else
{
iter->join();
}
#else
iter->join();
#endif
Expand Down Expand Up @@ -133,6 +143,35 @@ threadpool& threadpool::shared_instance()
return s_shared;
}

#elif defined (_WIN32) && defined(_WINDLL)

// if built as a DLL, the threadpool shared instance will be destroyed at DLL_PROCESS_DETACH,
// at which stage joining threads causes deadlock, hence this dance
threadpool& threadpool::shared_instance()
{
static bool terminate_threads = false;
static struct restore_terminate_threads
{
~restore_terminate_threads()
{
boost::asio::detail::win_thread::set_terminate_threads(terminate_threads);
}
} destroyed_after;

static threadpool_impl s_shared(40);

static struct enforce_terminate_threads
{
~enforce_terminate_threads()
{
terminate_threads = boost::asio::detail::win_thread::terminate_threads();
boost::asio::detail::win_thread::set_terminate_threads(true);
}
} destroyed_before;

return s_shared;
}

#else

// initialize the static shared threadpool
Expand Down