From b7c7558e1f55d62524642a5dd9847e75561b8b73 Mon Sep 17 00:00:00 2001 From: surtur Date: Fri, 21 Jan 2022 02:54:15 +0100 Subject: [PATCH] DoTask: timed_mutex -> recursive_timed_mutex --- do_task.cpp | 9 ++++++--- do_task.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/do_task.cpp b/do_task.cpp index 3993698..a9c203c 100644 --- a/do_task.cpp +++ b/do_task.cpp @@ -8,7 +8,7 @@ namespace fortuna { auto DoTask::die_pls() -> void { - do_sleep.unlock(); + mtx_do_sleep.unlock(); th.join(); } @@ -19,9 +19,12 @@ auto DoTask::thread_pls(const std::chrono::seconds& interval, die_pls(); } - do_sleep.lock(); + // since this is a std::recursive_timed_mutex, an attempt is made to only + // try_lock() it + mtx_do_sleep.try_lock(); + th = std::thread([this, interval, callback = std::move(callback)] { - while (!do_sleep.try_lock_for(interval)) { + while (!mtx_do_sleep.try_lock_for(interval)) { callback(); } }); diff --git a/do_task.h b/do_task.h index 94a29a5..c9ebee4 100644 --- a/do_task.h +++ b/do_task.h @@ -10,7 +10,7 @@ namespace fortuna { class DoTask { private: - std::timed_mutex do_sleep; + std::recursive_timed_mutex mtx_do_sleep; std::thread th; public: