![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
Runtime thread pool executing Task instances with priorities and FIFO ordering. More...
#include <dfx-runtime/ThreadPool.hpp>
Public Types | |
| using | MaybeThreadData = std::optional<std::reference_wrapper<ThreadData>> |
| Optional reference to mutable thread data. | |
| using | MaybeCThreadData = std::optional<std::reference_wrapper<ThreadData const>> |
| Optional reference to immutable thread data. | |
Public Member Functions | |
| ThreadPool (std::size_t threadCount=1) | |
| Construct a thread pool with an initial number of worker threads. | |
| ~ThreadPool () | |
| Stop all worker threads and abandon remaining tasks. | |
| void | addThread (std::size_t count=1) |
| Add worker threads to the pool. | |
| void | removeThread (std::size_t count=1) |
| Remove worker threads from the pool. | |
| std::size_t | threadCount () const noexcept |
| Current number of worker threads. | |
| void | pushTask (TaskPtr task) |
| Submit a task for execution. | |
| void | pushTasks (std::vector< TaskPtr > tasks) |
| Submit multiple tasks for execution. | |
| template<DerivedFromTask T, typename ... Args> requires std::is_constructible_v<T, Args...> | |
| void | emplaceTask (Args &&... args) |
| Construct a task in-place and submit it. | |
| MaybeThreadData | threadData () |
| Retrieve the calling thread's ThreadData if it belongs to this pool. | |
| MaybeCThreadData | threadData () const |
| Retrieve the calling thread's ThreadData if it belongs to this pool. | |
Runtime thread pool executing Task instances with priorities and FIFO ordering.
ThreadPool owns a set of worker threads (std::jthread) that continuously pull Task objects from an internal queue and execute them.
Tasks are scheduled using a priority first policy:
This allows tasks to access runtime thread metadata in a thread-independent way, without relying on thread_local storage or global maps.
| using dfx::Runtime::ThreadPool::MaybeCThreadData = std::optional<std::reference_wrapper<ThreadData const>> |
Optional reference to immutable thread data.
| using dfx::Runtime::ThreadPool::MaybeThreadData = std::optional<std::reference_wrapper<ThreadData>> |
Optional reference to mutable thread data.
| dfx::Runtime::ThreadPool::ThreadPool | ( | std::size_t | threadCount = 1 | ) |
Construct a thread pool with an initial number of worker threads.
A value of 0 is not allowed and will throw an exception.
| threadCount | Number of worker threads to start immediately. |
| dfx::Runtime::ThreadPool::~ThreadPool | ( | ) |
Stop all worker threads and abandon remaining tasks.
The pool stops its std::jthread workers (cooperative stop via stop tokens). Remaining task if any will not be run and thread will stop as soon as their current task is finished.
| void dfx::Runtime::ThreadPool::addThread | ( | std::size_t | count = 1 | ) |
Add worker threads to the pool.
Each new worker will create its own ThreadData on start.
| count | Number of threads to create and start. |
|
inline |
Construct a task in-place and submit it.
| T | Concrete Task type. |
| Args | Constructor argument types. |
| args | Arguments forwarded to T's constructor. |
| void dfx::Runtime::ThreadPool::pushTask | ( | TaskPtr | task | ) |
Submit a task for execution.
| task | Task to enqueue. Must not be null. |
| void dfx::Runtime::ThreadPool::pushTasks | ( | std::vector< TaskPtr > | tasks | ) |
Submit multiple tasks for execution.
| tasks | Tasks to enqueue. |
| void dfx::Runtime::ThreadPool::removeThread | ( | std::size_t | count = 1 | ) |
Remove worker threads from the pool.
There are two important behaviors:
count would remove too many threads, it is clamped so that at least one worker remains.| count | Requested number of threads to stop. |
|
inlinenoexcept |
Current number of worker threads.
| MaybeThreadData dfx::Runtime::ThreadPool::threadData | ( | ) |
Retrieve the calling thread's ThreadData if it belongs to this pool.
| MaybeCThreadData dfx::Runtime::ThreadPool::threadData | ( | ) | const |
Retrieve the calling thread's ThreadData if it belongs to this pool.