From 4e771ecfb73a96fe1cf245cdb1047f2be92c499e Mon Sep 17 00:00:00 2001 From: yuyoyuppe Date: Tue, 12 Nov 2019 18:29:54 +0300 Subject: [PATCH] initialize all OnThreadExecutor fields and clarify intent further (#701) --- src/common/on_thread_executor.cpp | 13 +++++++------ src/common/on_thread_executor.h | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/common/on_thread_executor.cpp b/src/common/on_thread_executor.cpp index dddcec5399..ef212cfb15 100644 --- a/src/common/on_thread_executor.cpp +++ b/src/common/on_thread_executor.cpp @@ -3,7 +3,8 @@ #include "on_thread_executor.h" OnThreadExecutor::OnThreadExecutor() - :_worker_thread{[this]() { worker_thread(); }} + : _shutdown_request{false} + , _worker_thread{[this] { worker_thread(); }} {} std::future OnThreadExecutor::submit(task_t task) { @@ -15,13 +16,13 @@ std::future OnThreadExecutor::submit(task_t task) { } void OnThreadExecutor::worker_thread() { - while(_active) { + while(!_shutdown_request) { task_t task; { std::unique_lock task_lock{_task_mutex}; - _task_cv.wait(task_lock, [this] { return !_task_queue.empty() || !_active; }); - if(!_active) { - break; + _task_cv.wait(task_lock, [this] { return !_task_queue.empty() || _shutdown_request; }); + if(_shutdown_request) { + return; } task = std::move(_task_queue.front()); _task_queue.pop(); @@ -31,7 +32,7 @@ void OnThreadExecutor::worker_thread() { } OnThreadExecutor::~OnThreadExecutor() { - _active = false; + _shutdown_request = true; _task_cv.notify_one(); _worker_thread.join(); } diff --git a/src/common/on_thread_executor.h b/src/common/on_thread_executor.h index 1c537fb9f0..c7d29359ac 100644 --- a/src/common/on_thread_executor.h +++ b/src/common/on_thread_executor.h @@ -25,6 +25,6 @@ private: std::mutex _task_mutex; std::condition_variable _task_cv; - std::atomic_bool _active; + std::atomic_bool _shutdown_request; std::queue> _task_queue; };