From 7b1aeee4565fe0a2eed4a4fa8695b2a5fb671e06 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 17 May 2026 14:32:11 +0100 Subject: Add ThreadSafeT helper Wraps a templated value and a templated mutex (defaults to shared_mutex) and provides safe access, locked with either a shared_lock (const value) or lock_guard (non-const value). Applies this to existingEntities. --- src/util.hpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/util.hpp') diff --git a/src/util.hpp b/src/util.hpp index f7254e8..5cac5a3 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace WebStat { @@ -95,4 +96,52 @@ namespace WebStat { } return false; } + + template class ThreadSafeT { + public: + template ThreadSafeT(P &&... params) : value {std::forward

(params)...} { } + + template class Locked { + public: + Locked(LockedValueType & valueRef, MutexType & mutex) : value {valueRef}, lock {mutex} { } + + LockedValueType * + operator->() + { + return &value; + } + + private: + LockedValueType & value; + LockType lock; + }; + + Locked> + shared() const + { + return {value, mutex}; + } + + Locked> + unique() + { + return {value, mutex}; + } + + Locked> + operator->() const + { + return shared(); + } + + Locked> + operator()() + { + return unique(); + } + + private: + ValueType value; + mutable MutexType mutex; + }; } -- cgit v1.3