initialize all OnThreadExecutor fields and clarify intent further (#701)

This commit is contained in:
yuyoyuppe 2019-11-12 18:29:54 +03:00 committed by GitHub
parent be86cd4028
commit 4e771ecfb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

View File

@ -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<void> OnThreadExecutor::submit(task_t task) {
@ -15,13 +16,13 @@ std::future<void> 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();
}

View File

@ -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<std::packaged_task<void()>> _task_queue;
};