diff --git a/MessageControl/MessageControl.h b/MessageControl/MessageControl.h index ca305fa9..3e3b9c43 100644 --- a/MessageControl/MessageControl.h +++ b/MessageControl/MessageControl.h @@ -239,41 +239,36 @@ namespace Plugin { void Dispatch() { + std::vector attaching; + std::vector detaching; + _adminLock.Lock(); - bool done = false; - - while (done == false) { - Observers::iterator index = _observing.begin(); - - while (done == false) { - if (index->second == state::ATTACHING) { - const uint32_t id = index->first; - index->second = state::OBSERVING; - _adminLock.Unlock(); - _parent.Attach(id); - _adminLock.Lock(); - break; - } - else if (index->second == state::DETACHING) { - const uint32_t id = index->first; - _observing.erase(index); - _adminLock.Unlock(); - _parent.Detach(id); - _adminLock.Lock(); - break; - } - else { - index++; - - if (index == _observing.end()) { - done = true; - } - } + Observers::iterator index = _observing.begin(); + + while (index != _observing.end()) { + if (index->second == state::ATTACHING) { + attaching.emplace_back(index->first); + index->second = state::OBSERVING; + index++; + } + else if (index->second == state::DETACHING) { + detaching.emplace_back(index->first); + index = _observing.erase(index); + } + else { + index++; } } - + _adminLock.Unlock(); + + for (const uint32_t& id : detaching) { + _parent.Detach(id); + } + for (const uint32_t& id : attaching) { + _parent.Attach(id); + } } private: