12#include <condition_variable>
16#include <shared_mutex>
21#include "tasks/Task.hpp"
22#include "ThreadData.hpp"
23#include <dfx-utilities/MonotonicIdAllocator.hpp>
63 struct QueueItemCompare
65 bool operator()(QueueItem
const & a, QueueItem
const & b)
const noexcept
68 auto const ap = a.task->priority();
69 auto const bp = b.task->priority();
113 std::size_t
threadCount() const noexcept { std::lock_guard lock(_mutexThreads);
return _threads.size(); }
130 requires std::is_constructible_v<T, Args...>
132 {
pushTask(std::make_unique<T>(std::forward<Args>(args)...)); }
153 void _work(std::stop_token stopToken);
156 std::vector<QueueItem> _queue;
157 std::mutex _mutexQueue;
159 std::condition_variable_any _cvQueue;
164 std::list<ThreadData> _threadDatas;
165 mutable std::shared_mutex _mutexThreadData;
168 mutable std::mutex _mutexThreads;
170 std::vector<std::jthread> _threads;
void emplaceTask(Args &&... args)
Construct a task in-place and submit it.
Definition ThreadPool.hpp:131
std::size_t threadCount() const noexcept
Current number of worker threads.
Definition ThreadPool.hpp:113
MaybeThreadData threadData()
Retrieve the calling thread's ThreadData if it belongs to this pool.
void pushTasks(std::vector< TaskPtr > tasks)
Submit multiple tasks for execution.
std::optional< std::reference_wrapper< ThreadData > > MaybeThreadData
Optional reference to mutable thread data.
Definition ThreadPool.hpp:136
void pushTask(TaskPtr task)
Submit a task for execution.
std::optional< std::reference_wrapper< ThreadData const > > MaybeCThreadData
Optional reference to immutable thread data.
Definition ThreadPool.hpp:138
MaybeCThreadData threadData() const
Retrieve the calling thread's ThreadData if it belongs to this pool.
void addThread(std::size_t count=1)
Add worker threads to the pool.
~ThreadPool()
Stop all worker threads and abandon remaining tasks.
ThreadPool(std::size_t threadCount=1)
Construct a thread pool with an initial number of worker threads.
void removeThread(std::size_t count=1)
Remove worker threads from the pool.
Monotonically increasing ID allocator.
Definition MonotonicIdAllocator.hpp:52
Convenience concept for constraining templates to Task-derived types.
Definition Task.hpp:24
std::unique_ptr< Task > TaskPtr
Unique ownership pointer for tasks.
Definition Task.hpp:102