mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-05 04:39:08 +08:00
initialize all OnThreadExecutor fields and clarify intent further (#701)
This commit is contained in:
parent
be86cd4028
commit
4e771ecfb7
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user