fix HandlersList concurrency

This commit is contained in:
MihailRis
2025-01-16 04:16:32 +03:00
parent 44b9e21d46
commit 2073d3782a
+9 -5
View File
@@ -14,7 +14,7 @@ namespace util {
int nextid = 1; int nextid = 1;
std::unordered_map<int, std::function<bool(Types...)>> handlers; std::unordered_map<int, std::function<bool(Types...)>> handlers;
std::vector<int> order; std::vector<int> order;
std::recursive_mutex mutex; std::mutex mutex;
public: public:
HandlersList() = default; HandlersList() = default;
@@ -46,11 +46,15 @@ namespace util {
} }
void notify(Types...args) { void notify(Types...args) {
std::lock_guard lock(mutex); std::vector<int> orderCopy;
decltype(handlers) handlersCopy;
auto orderCopy = order; {
std::lock_guard lock(mutex);
orderCopy = order;
handlersCopy = handlers;
}
for (auto it = orderCopy.rbegin(); it != orderCopy.rend(); ++it) { for (auto it = orderCopy.rbegin(); it != orderCopy.rend(); ++it) {
if (handlers.at(*it)(args...)) { if (handlersCopy.at(*it)(args...)) {
break; break;
} }
} }