From 69ee45f2fc4fe323b5c0ae381e5092ef27d9e08b Mon Sep 17 00:00:00 2001 From: Philip Rauwolf Date: Mon, 4 Mar 2013 17:35:36 +0100 Subject: [PATCH] Added mutex for event unsubscribe calls --- src/CommonAPI/Event.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/CommonAPI/Event.h b/src/CommonAPI/Event.h index 19a74fa..e79d491 100644 --- a/src/CommonAPI/Event.h +++ b/src/CommonAPI/Event.h @@ -10,6 +10,7 @@ #include #include #include +#include namespace CommonAPI { @@ -34,7 +35,7 @@ class Event { Subscription subscribeCancellableListener(CancellableListener listener); void unsubscribe(Subscription listenerSubscription); - virtual ~Event() { } + virtual ~Event() {} protected: // Returns false if all subscriptions were cancelled @@ -53,6 +54,7 @@ class Event { private: ListenersList listenersList_; + std::mutex listenerListMutex_; }; template @@ -94,7 +96,9 @@ template void Event<_Arguments...>::unsubscribe(Subscription listenerSubscription) { const CancellableListener cancellableListener = *listenerSubscription; + listenerListMutex_.lock(); listenersList_.erase(listenerSubscription); + listenerListMutex_.unlock(); onListenerRemoved(cancellableListener); @@ -105,6 +109,7 @@ void Event<_Arguments...>::unsubscribe(Subscription listenerSubscription) { template SubscriptionStatus Event<_Arguments...>::notifyListeners(const _Arguments&... eventArguments) { + listenerListMutex_.lock(); for (auto iterator = listenersList_.begin(); iterator != listenersList_.end(); ) { const CancellableListener& cancellableListener = *iterator; const SubscriptionStatus listenerSubscriptionStatus = cancellableListener(eventArguments...); @@ -116,6 +121,7 @@ SubscriptionStatus Event<_Arguments...>::notifyListeners(const _Arguments&... ev } else iterator++; } + listenerListMutex_.unlock(); return listenersList_.empty() ? SubscriptionStatus::CANCEL : SubscriptionStatus::RETAIN; } -- 2.7.4