Release lock before joining thread, so other threads don't have to wait

This commit is contained in:
Sebastian Messmer 2015-11-07 12:21:54 -08:00
parent 47bd752a97
commit c1734dd93e

View File

@ -26,9 +26,13 @@ namespace cpputils {
void ThreadSystem::stop(Handle handle) { void ThreadSystem::stop(Handle handle) {
boost::unique_lock<boost::mutex> lock(_mutex); boost::unique_lock<boost::mutex> lock(_mutex);
handle->thread.interrupt(); boost::thread thread = std::move(handle->thread);
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). thread.interrupt();
_runningThreads.erase(handle); _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() { void ThreadSystem::_onBeforeFork() {