|
| | ThreadPool (size_t thread_count=4U) |
| | Constructs a ThreadPool with thread_count worker threads and kNormalType queue.
|
| | ThreadPool (size_t thread_count, Type type) |
| | Constructs a ThreadPool with the specified thread count and queue type.
|
| virtual | ~ThreadPool () |
| | Destructor. Calls shutdown() and joins all worker threads.
|
| void | set_name (const std::string &name) |
| | Sets a human-readable name for the pool (and its worker threads).
|
| const std::string & | get_name () const |
| | Returns the name set via set_name().
|
| Type | get_type () const |
| | Returns the queue type this pool was constructed with.
|
| Strategy | get_strategy () const |
| | Returns the current idle strategy.
|
| void | set_strategy (Strategy strategy) |
| | Changes the idle strategy.
|
| bool | shutdown () |
| | Signals all workers to finish current tasks and exit.
|
| bool | post_task (Callback &&callback) |
| | Posts a task to the queue for execution by a worker thread.
|
| size_t | get_task_count () const |
| | Returns the number of tasks currently in the queue.
|
| bool | is_in_work_thread () const |
| | Returns true if the calling thread is a worker thread of this pool.
|
| virtual size_t | get_max_task_count () const |
| | Returns the maximum queue depth.
|
| template<class FunctionT, class... ArgsT, typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>> |
| std::future< ResultT > | invoke_task (FunctionT &&function, ArgsT &&... args) |
| | Dispatches a callable to a worker thread and returns a std::future for the result.
|
Fixed-size thread pool for parallel task execution.
Worker threads are created on construction and destroyed on shutdown() or destruction.
template<class FunctionT, class... ArgsT, typename ResultT>
| std::future< ResultT > vlink::ThreadPool::invoke_task |
( |
FunctionT && | function, |
|
|
ArgsT &&... | args ) |
|
inlinenodiscard |
Dispatches a callable to a worker thread and returns a std::future for the result.
Details
Thread-safe. The future becomes ready after the callable is executed by a worker.
- 警告
- Do not block on the future from a pool worker thread if all workers are busy; this will deadlock.
- 模板参数
-
| FunctionT | Callable type. |
| ArgsT | Argument types. |
| ResultT | Return type (deduced). |
- 参数
-
| function | Callable to dispatch. |
| args | Arguments forwarded to the callable. |
- 返回
std::future<ResultT> that becomes ready when the task completes.