Skip to content

Commit

Permalink
initialize all OnThreadExecutor fields and clarify intent further (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyoyuppe committed Nov 12, 2019
1 parent be86cd4 commit 4e771ec
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/common/on_thread_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -31,7 +32,7 @@ void OnThreadExecutor::worker_thread() {
}

OnThreadExecutor::~OnThreadExecutor() {
_active = false;
_shutdown_request = true;
_task_cv.notify_one();
_worker_thread.join();
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/on_thread_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ class OnThreadExecutor final {

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;
};

0 comments on commit 4e771ec

Please sign in to comment.