Lock before adding or removing message callbacks

This commit is contained in:
Paul Hollinsky
2019-06-13 15:47:23 -04:00
parent 4530bd14c0
commit b7dbeccaef
2 changed files with 5 additions and 1 deletions
+4 -1
View File
@@ -91,11 +91,13 @@ std::shared_ptr<SerialNumberMessage> Communication::getSerialNumberSync(std::chr
}
int Communication::addMessageCallback(const MessageCallback& cb) {
std::lock_guard<std::mutex> lk(messageCallbacksLock);
messageCallbacks.insert(std::make_pair(messageCallbackIDCounter, cb));
return messageCallbackIDCounter++;
}
bool Communication::removeMessageCallback(int id) {
std::lock_guard<std::mutex> lk(messageCallbacksLock);
try {
messageCallbacks.erase(id);
return true;
@@ -140,7 +142,8 @@ void Communication::readTask() {
err(APIError::Unknown); // TODO Use specific error
continue;
}
std::lock_guard<std::mutex> lk(messageCallbacksLock);
for(auto& cb : messageCallbacks) {
if(!closing) { // We might have closed while reading or processing
cb.second.callIfMatch(msg);