From 048cb16612b6a77abfb9838b3b2d20f1f10c193a Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 30 Nov 2017 10:21:51 +0100 Subject: [PATCH] Fixes freeze when debugging failed to start: Dispatcher::post can now be performed within another Dispatcher::post, no longer leading to deadlock --- src/dispatcher.cc | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/dispatcher.cc b/src/dispatcher.cc index e45d4d8..5bed8ea 100644 --- a/src/dispatcher.cc +++ b/src/dispatcher.cc @@ -1,18 +1,27 @@ #include "dispatcher.h" +#include Dispatcher::Dispatcher() { connection=dispatcher.connect([this] { - std::unique_lock lock(functions_mutex); - for(auto &function: functions) - function(); - functions.clear(); + std::vector>::iterator> its; + { + std::unique_lock lock(functions_mutex); + its.reserve(functions.size()); + for(auto it=functions.begin();it!=functions.end();++it) + its.emplace_back(it); + } + for(auto &it: its) + (*it)(); + { + std::unique_lock lock(functions_mutex); + for(auto &it: its) + functions.erase(it); + } }); } Dispatcher::~Dispatcher() { disconnect(); - std::unique_lock lock(functions_mutex); - functions.clear(); } void Dispatcher::disconnect() {