85 #include <type_traits>
131 kOptimizationStrategy = 0,
285 template <
class FunctionT,
class... ArgsT,
typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>>
286 [[nodiscard]] std::future<ResultT> invoke_task(FunctionT&&
function, ArgsT&&... args);
291 bool push_task(
Callback&& callback,
bool droppable,
295 bool drop_one_normal_task();
297 bool drop_one_lockfree_task(
bool keep_reserved =
false);
299 bool reserve_lockfree_task(
bool* was_empty =
nullptr);
301 void release_lockfree_task();
303 bool push_lockfree_task(
Callback&& callback);
306 std::shared_ptr<Impl> impl_;
315 template <
class FunctionT,
class... ArgsT,
typename ResultT>
317 auto bound = [
function = std::forward<FunctionT>(
function),
318 args = std::make_tuple(std::forward<ArgsT>(args)...)]()
mutable -> ResultT {
320 [&
function](
auto&&... unpacked_args) -> ResultT {
321 return std::invoke(
function, std::forward<decltype(unpacked_args)>(unpacked_args)...);
326 if constexpr (kIsSupportMoveFunction) {
327 std::packaged_task<ResultT()> task(std::move(bound));
328 auto res = task.get_future();
337 auto res = task->get_future();
Shared observable handle returned by tracked posting APIs.
Definition: task_handle.h:189
Fixed worker pool that consumes a shared task queue.
Definition: thread_pool.h:103
Strategy get_strategy() const
Returns the current submission-side back-pressure strategy.
size_t get_task_count() const
Returns the current number of tasks waiting in the queue.
ThreadPool(size_t thread_count=4U)
Constructs a pool with thread_count workers and the default kNormalType queue.
bool post_task(Callback &&callback)
Submits a task to the queue for execution by a worker thread.
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.
const std::string & get_name() const
Returns the display name assigned via set_name().
Strategy
Submission-side strategy applied when the bounded queue is full.
Definition: thread_pool.h:130
void set_strategy(Strategy strategy)
Updates the submission-side back-pressure strategy.
Type
Queue implementation backing the pool.
Definition: thread_pool.h:117
void set_name(const std::string &name)
Assigns a human-readable name to the pool and its workers (used by debuggers).
bool shutdown()
Marks the pool as quitting, drains in-flight tasks, and joins workers.
virtual ~ThreadPool()
Destructor. Calls shutdown() and joins worker threads.
ThreadPool(size_t thread_count, Type type)
Constructs a pool with a custom queue implementation.
Type get_type() const
Returns the queue implementation used by this pool.
std::future< ResultT > invoke_task(FunctionT &&function, ArgsT &&... args)
Submits a callable to a worker thread and returns a std::future for the result.
Definition: thread_pool.h:316
TaskHandle post_task_handle(Callback &&callback, const PostTaskOptions &options={})
Submits a task that produces an observable TaskHandle.
Pool-backed type-erased callables: copyable vlink::Function and move-only vlink::MoveFunction.
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:289
#define VLINK_EXPORT
Definition: macros.h:81
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
PMR adapter that lets standard pmr-aware containers allocate through vlink::MemoryPool.
std::shared_ptr< T > make_shared(Args &&... args)
Fallback make_shared forwarding to std::make_shared.
Definition: memory_resource.h:276
TaskOverflowPolicy
Per-post override for the bounded-queue overflow strategy.
Definition: task_handle.h:128
@ kUseDispatcherStrategy
Inherit the dispatcher-configured overflow strategy.
Bundle of optional knobs accepted by tracked task-posting APIs.
Definition: task_handle.h:156
Observable handle returned by tracked task-posting APIs of MessageLoop and ThreadPool.