From ed59c3f77f198922be06bb6c96839e9ad218f657 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 29 Jan 2017 19:07:31 +0100 Subject: [PATCH] Made mutex into a value instead of a pointer in ConcurrentMap --- concurrentmap.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/concurrentmap.h b/concurrentmap.h index 9dd16f343..3ce3e31bf 100644 --- a/concurrentmap.h +++ b/concurrentmap.h @@ -16,58 +16,57 @@ public: ConcurrentMap() : map() { - this->mutex = new QMutex(); } bool tryGet(const TKey &name, TValue &value) const { - this->mutex->lock(); + this->mutex.lock(); auto a = map.find(name); if (a == map.end()) { - this->mutex->unlock(); + this->mutex.unlock(); return false; } value = a.value(); - this->mutex->unlock(); + this->mutex.unlock(); return true; } TValue getOrAdd(const TKey &name, std::function addLambda) { - this->mutex->lock(); + this->mutex.lock(); auto a = map.find(name); if (a == map.end()) { TValue value = addLambda(); map.insert(name, value); - this->mutex->unlock(); + this->mutex.unlock(); return value; } - this->mutex->unlock(); + this->mutex.unlock(); return a.value(); } void clear() { - this->mutex->lock(); + this->mutex.lock(); map.clear(); - this->mutex->unlock(); + this->mutex.unlock(); } void insert(const TKey &name, const TValue &value) { - this->mutex->lock(); + this->mutex.lock(); map.insert(name, value); - this->mutex->unlock(); + this->mutex.unlock(); } private: - QMutex *mutex; + mutable QMutex mutex; QMap map; }; }