From 96db0ad5079bc742fb07b9fa8280ea889ec092df Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Thu, 28 Mar 2024 20:53:07 +0100 Subject: [PATCH] Fix crash when stopping queue from within the queue --- lib/utils/action-queue.cpp | 12 ++++++++++-- lib/utils/action-queue.hpp | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/utils/action-queue.cpp b/lib/utils/action-queue.cpp index 3fdd7959..f427c1b0 100644 --- a/lib/utils/action-queue.cpp +++ b/lib/utils/action-queue.cpp @@ -51,9 +51,13 @@ void ActionQueue::Load(obs_data_t *obj) void ActionQueue::Start() { - if (_thread.joinable()) { + if (!_stop) { return; } + + if (_thread.joinable()) { + _thread.join(); + } _stop = false; _thread = std::thread(&ActionQueue::RunActions, this); } @@ -62,6 +66,10 @@ void ActionQueue::Stop() { _stop = true; _cv.notify_all(); + if (std::this_thread::get_id() == _thread.get_id()) { + return; + } + if (_thread.joinable()) { _thread.join(); } @@ -69,7 +77,7 @@ void ActionQueue::Stop() bool ActionQueue::IsRunning() const { - return _thread.joinable(); + return !_stop; } bool ActionQueue::RunsOnStartup() const diff --git a/lib/utils/action-queue.hpp b/lib/utils/action-queue.hpp index 0ef41444..0d4e8c8c 100644 --- a/lib/utils/action-queue.hpp +++ b/lib/utils/action-queue.hpp @@ -39,7 +39,7 @@ private: bool _runOnStartup = true; bool _resolveVariablesOnAdd = true; - std::atomic_bool _stop = {false}; + std::atomic_bool _stop = {true}; std::mutex _mutex; std::condition_variable _cv; std::thread _thread;