219 template <
class FunctionT,
class... ArgsT,
typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>>
220 [[nodiscard]] std::future<ResultT>
invoke_task(FunctionT&& function, ArgsT&&... args);
225 std::unique_ptr<struct ThreadPoolImpl> impl_;
234template <
class FunctionT,
class... ArgsT,
typename ResultT>
236 auto task = std::make_shared<std::packaged_task<ResultT()>>(
237 std::bind(std::forward<FunctionT>(function), std::forward<ArgsT>(args)...));
239 std::future<ResultT> res = task->get_future();
Strategy get_strategy() const
Returns the current idle strategy.
size_t get_task_count() const
Returns the number of tasks currently in the queue.
ThreadPool(size_t thread_count=4U)
Constructs a ThreadPool with thread_count worker threads and kNormalType queue.
bool post_task(Callback &&callback)
Posts a task to the queue for execution by a worker thread.
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.
Strategy
Idle strategy controlling CPU and wake-up latency trade-offs.
Definition thread_pool.h:98
@ kPopStrategy
Busy-poll (lowest latency, highest CPU).
Definition thread_pool.h:100
@ kOptimizationStrategy
Balance latency and CPU via yield.
Definition thread_pool.h:99
@ kBlockStrategy
Block on condition variable (lowest CPU).
Definition thread_pool.h:101
void set_strategy(Strategy strategy)
Changes the idle strategy.
Type
Queue implementation type.
Definition thread_pool.h:90
@ kNormalType
Mutex-protected FIFO queue (default).
Definition thread_pool.h:91
@ kLockfreeType
Lock-free MPMC queue.
Definition thread_pool.h:92
void set_name(const std::string &name)
Sets a human-readable name for the pool (and its worker threads).
bool shutdown()
Signals all workers to finish current tasks and exit.
const std::string & get_name() const
Returns the name set via set_name().
std::function< void()> Callback
Callback type for tasks submitted to the pool.
Definition thread_pool.h:85
virtual ~ThreadPool()
Destructor. Calls shutdown() and joins all worker threads.
ThreadPool(size_t thread_count, Type type)
Constructs a ThreadPool with the specified thread count and queue type.
Type get_type() const
Returns the queue type this pool was constructed with.
std::future< ResultT > invoke_task(FunctionT &&function, ArgsT &&... args)
Dispatches a callable to a worker thread and returns a std::future for the result.
Definition thread_pool.h:235
Platform-independent macro definitions for the VLink library.
#define VLINK_EXPORT
Definition macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184