mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-21 07:17:56 +08:00
6161531376
* Made drawing zones not clog message processing * Fixed synchronization bugs * call BufferedPaintInit * Some refactoring, the animation is back * Rename a function * Align parameters
52 lines
1.1 KiB
C++
52 lines
1.1 KiB
C++
#include "pch.h"
|
|
|
|
#include "on_thread_executor.h"
|
|
|
|
OnThreadExecutor::OnThreadExecutor() :
|
|
_shutdown_request{ false }, _worker_thread{ [this] { worker_thread(); } }
|
|
{
|
|
}
|
|
|
|
std::future<void> OnThreadExecutor::submit(task_t task)
|
|
{
|
|
auto future = task.get_future();
|
|
std::lock_guard lock{ _task_mutex };
|
|
_task_queue.emplace(std::move(task));
|
|
_task_cv.notify_one();
|
|
return future;
|
|
}
|
|
|
|
void OnThreadExecutor::cancel()
|
|
{
|
|
std::lock_guard lock{ _task_mutex };
|
|
_task_queue = {};
|
|
_task_cv.notify_one();
|
|
}
|
|
|
|
|
|
void OnThreadExecutor::worker_thread()
|
|
{
|
|
while (!_shutdown_request)
|
|
{
|
|
task_t task;
|
|
{
|
|
std::unique_lock task_lock{ _task_mutex };
|
|
_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();
|
|
}
|
|
task();
|
|
}
|
|
}
|
|
|
|
OnThreadExecutor::~OnThreadExecutor()
|
|
{
|
|
_shutdown_request = true;
|
|
_task_cv.notify_one();
|
|
_worker_thread.join();
|
|
}
|