VLink  2.0.0
A high-performance communication middleware
thread_pool.h 文件参考

Fixed-size worker pool for parallel task execution. 更多...

#include <functional>
#include <future>
#include <memory>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include "./functional.h"
#include "./macros.h"
#include "./memory_resource.h"
#include "./task_handle.h"
thread_pool.h 的引用(Include)关系图:

浏览源代码.

class  vlink::ThreadPool
 Fixed worker pool that consumes a shared task queue. 更多...
 

命名空间

 

详细描述

Fixed-size worker pool for parallel task execution.

vlink::ThreadPool launches a configurable number of worker threads at construction and dispatches submitted tasks to whichever worker is idle. Unlike MessageLoop, the pool has no built-in timer mechanism or loop lifecycle; it is started immediately and torn down with shutdown(). A pool created with zero workers is left in the shutdown state and rejects every submission.

Architecture:

*     post_task() ----->  +---------------------------+
*                         | shared dispatcher queue   |
*                         +-----+----------+----------+
*                               |          |          |
*                               v          v          v
*                            worker 1   worker 2 ... worker N
*                               |          |          |
*                               v          v          v
*                            user task  user task  user task
* 

Queue implementations:

Type Backing queue Notes
kNormalType Mutex-protected std::deque (or std::pmr) Default; honours drop policy
kLockfreeType MpmcQueue (lock-free multi-producer/consumer) Lower contention overhead

Push-side back-pressure strategies are identical to MessageLoop::Strategy. They only affect submission when the queue is full; worker wake-up is always driven by a condition variable.

注解
  • Tasks may run concurrently; protect shared state externally.
  • invoke_task() returns a std::future; blocking on it from a pool worker can deadlock if every worker is busy.
  • is_in_work_thread() can detect re-entrant submissions.
Example
pool.post_task([] { heavy_work(); });
auto fut = pool.invoke_task([]() -> int { return compute(); });
int result = fut.get();
pool.shutdown();