|
VLink
2.0.0
A high-performance communication middleware
|
Fixed worker pool that consumes a shared task queue. 更多...
#include <thread_pool.h>
Public 类型 | |
| enum | Type : uint8_t { kNormalType = 0 , kLockfreeType = 1 } |
| Queue implementation backing the pool. 更多... | |
| enum | Strategy : uint8_t { kOptimizationStrategy = 0 , kPopStrategy = 1 , kBlockStrategy = 2 } |
| Submission-side strategy applied when the bounded queue is full. 更多... | |
| using | Callback = MoveFunction< void()> |
| Callback signature used for submitted tasks. 更多... | |
Public 成员函数 | |
| ThreadPool (size_t thread_count=4U) | |
Constructs a pool with thread_count workers and the default kNormalType queue. 更多... | |
| ThreadPool (size_t thread_count, Type type) | |
| Constructs a pool with a custom queue implementation. 更多... | |
| virtual | ~ThreadPool () |
Destructor. Calls shutdown() and joins worker threads. 更多... | |
| void | set_name (const std::string &name) |
| Assigns a human-readable name to the pool and its workers (used by debuggers). 更多... | |
| const std::string & | get_name () const |
Returns the display name assigned via set_name(). 更多... | |
| Type | get_type () const |
| Returns the queue implementation used by this pool. 更多... | |
| Strategy | get_strategy () const |
| Returns the current submission-side back-pressure strategy. 更多... | |
| void | set_strategy (Strategy strategy) |
| Updates the submission-side back-pressure strategy. 更多... | |
| bool | shutdown () |
| Marks the pool as quitting, drains in-flight tasks, and joins workers. 更多... | |
| bool | post_task (Callback &&callback) |
| Submits a task to the queue for execution by a worker thread. 更多... | |
| TaskHandle | post_task_handle (Callback &&callback, const PostTaskOptions &options={}) |
Submits a task that produces an observable TaskHandle. 更多... | |
| size_t | get_task_count () const |
| Returns the current number of tasks waiting in the queue. 更多... | |
| bool | is_in_work_thread () const |
| Reports whether the calling thread is one of this pool's workers. 更多... | |
| virtual size_t | get_max_task_count () const |
| Returns the maximum queue capacity. 更多... | |
| template<class FunctionT , class... ArgsT, typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>> | |
| std::future< ResultT > | invoke_task (FunctionT &&function, ArgsT &&... args) |
Submits a callable to a worker thread and returns a std::future for the result. 更多... | |
Fixed worker pool that consumes a shared task queue.
Worker threads are created during construction and joined during shutdown() or destruction. The pool object itself is non-copyable.
| using vlink::ThreadPool::Callback = MoveFunction<void()> |
Callback signature used for submitted tasks.
Move-only (MoveFunction<void()>); see MessageLoop::Callback for rationale.
| enum vlink::ThreadPool::Strategy : uint8_t |
Submission-side strategy applied when the bounded queue is full.
Worker wake-up is independent of this enum. It controls only how post_task and invoke_task react when capacity is reached.
| enum vlink::ThreadPool::Type : uint8_t |
|
explicit |
Constructs a pool with thread_count workers and the default kNormalType queue.
| thread_count | Worker thread count. Default: 4. Zero leaves the pool in the shutdown state. |
|
explicit |
Constructs a pool with a custom queue implementation.
| thread_count | Worker thread count. Zero leaves the pool in the shutdown state. |
| type | Queue implementation type. |
|
virtual |
Destructor. Calls shutdown() and joins worker threads.
|
virtual |
Returns the maximum queue capacity.
| const std::string& vlink::ThreadPool::get_name | ( | ) | const |
Returns the display name assigned via set_name().
| Strategy vlink::ThreadPool::get_strategy | ( | ) | const |
Returns the current submission-side back-pressure strategy.
| size_t vlink::ThreadPool::get_task_count | ( | ) | const |
Returns the current number of tasks waiting in the queue.
| Type vlink::ThreadPool::get_type | ( | ) | const |
Returns the queue implementation used by this pool.
|
inline |
Submits a callable to a worker thread and returns a std::future for the result.
Details
Thread-safe. The future is satisfied once the callable returns. When posting fails the future becomes ready with a broken_promise / future_error result.
| FunctionT | Callable type. |
| ArgsT | Argument types forwarded to the callable. |
| ResultT | Deduced result type. |
| function | Callable to dispatch. |
| args | Arguments to forward. |
| bool vlink::ThreadPool::is_in_work_thread | ( | ) | const |
Reports whether the calling thread is one of this pool's workers.
true when called from a worker. | bool vlink::ThreadPool::post_task | ( | Callback && | callback | ) |
Submits a task to the queue for execution by a worker thread.
Thread-safe. Returns false when the pool is already shut down or when overflow handling cannot make room for the new task. Overflow behaviour depends on the configured Strategy:
kOptimizationStrategy: retry up to 10 times with a 1 ms sleep, then drop one eligible task and push the new one.kPopStrategy: drop one eligible task immediately and push the new one.kBlockStrategy: retry indefinitely with 1 ms sleep until space is available.kNormalType respects TaskDropPolicy::kProtected; protected tasks are never selected as eviction victims, and if every queued task is protected the post fails and returns false.kLockfreeType does not track per-task drop policy; overflow drop simply removes one queued task regardless of how it was submitted.| callback | Task to execute. |
true when the task was eventually enqueued. | TaskHandle vlink::ThreadPool::post_task_handle | ( | Callback && | callback, |
| const PostTaskOptions & | options = {} |
||
| ) |
Submits a task that produces an observable TaskHandle.
Tracked counterpart of post_task(). The returned handle allows callers to wait for completion, request cooperative cancellation, and observe whether the task was rejected or dropped before execution.
| callback | Task to execute. |
| options | Optional overflow, drop, and cancellation policy. |
state(). | void vlink::ThreadPool::set_name | ( | const std::string & | name | ) |
Assigns a human-readable name to the pool and its workers (used by debuggers).
| name | Display name. |
| void vlink::ThreadPool::set_strategy | ( | Strategy | strategy | ) |
Updates the submission-side back-pressure strategy.
| strategy | New strategy. |
| bool vlink::ThreadPool::shutdown | ( | ) |
Marks the pool as quitting, drains in-flight tasks, and joins workers.
After shutdown() returns, further submissions are rejected. Workers complete the task they are currently running plus any already-queued tasks before exiting. When called from a worker thread the calling worker's handle is detached instead of joined because a thread cannot join itself; the Impl block is kept alive via std::shared_ptr so the detached worker continues to see a valid pool state until it returns.
true on the first successful shutdown; false on subsequent calls.