26#ifndef BACKGROUNDQUEUE_H
27#define BACKGROUNDQUEUE_H
30#include <condition_variable>
73 : m_fn(std::move(fn)) {}
78 enum class State { Queued, Running, Done };
80 std::function<void(stop_token)> m_fn;
81 stop_source m_stopSource;
82 mutable std::mutex m_mutex;
83 std::condition_variable m_doneCond;
84 State m_state = State::Queued;
106 static std::shared_ptr<BackgroundTask>
121 void Enqueue(
const std::shared_ptr<BackgroundTask> &task,
131 std::condition_variable m_cond;
132 std::deque<std::shared_ptr<BackgroundTask>> m_high;
133 std::deque<std::shared_ptr<BackgroundTask>> m_low;
134 std::vector<std::thread> m_workers;
135 bool m_shutdown =
false;
A process-wide thread pool that runs BackgroundTasks, highest priority first.
Definition: BackgroundQueue.h:98
static int Workers()
The number of worker threads the pool runs.
Definition: BackgroundQueue.cpp:75
static std::shared_ptr< BackgroundTask > Add(BackgroundTask::Priority priority, std::function< void(stop_token)> task)
Enqueue a task at the given priority and return a handle to it.
Definition: BackgroundQueue.cpp:125
A unit of work that has been handed to the BackgroundQueue.
Definition: BackgroundQueue.h:46
Priority
The two priority levels the queue serves. High is always served first.
Definition: BackgroundQueue.h:50
void RequestStop()
Ask the task to stop at its next cancellation point. If it has not been started yet it is dropped fro...
Definition: BackgroundQueue.cpp:50
bool Pending() const
Whether the task is still waiting in the queue or currently running.
Definition: BackgroundQueue.cpp:63
void Wait()
Block until the task has finished (or was dropped before it started). Returns immediately if that alr...
Definition: BackgroundQueue.cpp:57