From c1734dd93e2f55f317fe5949749c839d4757cf0a Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sat, 7 Nov 2015 12:21:54 -0800 Subject: [PATCH] Release lock before joining thread, so other threads don't have to wait --- thread/ThreadSystem.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/thread/ThreadSystem.cpp b/thread/ThreadSystem.cpp index f5cca1c3..f9dbbbaf 100644 --- a/thread/ThreadSystem.cpp +++ b/thread/ThreadSystem.cpp @@ -26,9 +26,13 @@ namespace cpputils { void ThreadSystem::stop(Handle handle) { boost::unique_lock lock(_mutex); - handle->thread.interrupt(); - handle->thread.join(); //TODO Can I release the lock before calling join()? Maybe I have to move the erase() line to earlier (inside the lock). + boost::thread thread = std::move(handle->thread); + thread.interrupt(); _runningThreads.erase(handle); + + //It's fine if another thread gets the mutex while we still wait for the join. Joining doesn't change any internal state. + lock.unlock(); + thread.join(); } void ThreadSystem::_onBeforeFork() {