From 685e28d161be084be2aa4d603083a049f250cd69 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sun, 11 Aug 2024 19:14:35 +0200 Subject: [PATCH] Make read and write functions of variables thread-safe --- lib/variables/variable.cpp | 2 ++ lib/variables/variable.hpp | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/variables/variable.cpp b/lib/variables/variable.cpp index 44cf5358..5ea0942a 100644 --- a/lib/variables/variable.cpp +++ b/lib/variables/variable.cpp @@ -54,6 +54,7 @@ void Variable::Save(obs_data_t *obj) const std::string Variable::Value(bool updateLastUsed) const { + std::lock_guard lock(_mutex); if (updateLastUsed) { UpdateLastUsed(); } @@ -73,6 +74,7 @@ std::optional Variable::IntValue() const void Variable::SetValue(const std::string &value) { + std::lock_guard lock(_mutex); _previousValue = _value; _value = value; diff --git a/lib/variables/variable.hpp b/lib/variables/variable.hpp index 4bdf1c91..1f45e71b 100644 --- a/lib/variables/variable.hpp +++ b/lib/variables/variable.hpp @@ -3,10 +3,11 @@ #include "item-selection-helpers.hpp" #include "resizing-text-edit.hpp" -#include -#include -#include +#include #include +#include +#include +#include namespace advss { @@ -52,6 +53,7 @@ private: int _valueChangeCount = 0; mutable std::chrono::high_resolution_clock::time_point _lastUsed; mutable std::chrono::high_resolution_clock::time_point _lastChanged; + mutable std::mutex _mutex; friend VariableSelection; friend VariableSettingsDialog;