Lightweight 0.20260617.0
Loading...
Searching...
No Matches
Lightweight::Async::ThreadPoolExecutor Class Referencefinal

#include <ThreadPoolExecutor.hpp>

Inheritance diagram for Lightweight::Async::ThreadPoolExecutor:
Lightweight::Async::IExecutor Lightweight::Async::IResumeScheduler

Public Member Functions

 ThreadPoolExecutor (std::size_t threadCount)
 
 ThreadPoolExecutor (ThreadPoolExecutor const &)=delete
 
ThreadPoolExecutoroperator= (ThreadPoolExecutor const &)=delete
 
 ThreadPoolExecutor (ThreadPoolExecutor &&)=delete
 
ThreadPoolExecutoroperator= (ThreadPoolExecutor &&)=delete
 
 ~ThreadPoolExecutor () override
 
void Post (Work work) override
 
void Resume (std::coroutine_handle<> handle) override
 
std::size_t ThreadCount () const noexcept
 
- Public Member Functions inherited from Lightweight::Async::IExecutor
 IExecutor (IExecutor const &)=delete
 
IExecutoroperator= (IExecutor const &)=delete
 
 IExecutor (IExecutor &&)=delete
 
IExecutoroperator= (IExecutor &&)=delete
 
- Public Member Functions inherited from Lightweight::Async::IResumeScheduler
 IResumeScheduler (IResumeScheduler const &)=delete
 
IResumeScheduleroperator= (IResumeScheduler const &)=delete
 
 IResumeScheduler (IResumeScheduler &&)=delete
 
IResumeScheduleroperator= (IResumeScheduler &&)=delete
 

Detailed Description

A fixed-size pool of worker threads that run posted work concurrently.

This is the default "DB worker" offload target: blocking ODBC calls are posted here and executed on a worker thread while the awaiting coroutine is suspended. The pool is built on stdexec's exec::static_thread_pool (the C++26 std::execution scheduler model); each posted Work item is spawned as a schedule|then sender into an exec::async_scope so the destructor can wait for every in-flight item to finish, draining and joining. The pool must therefore outlive every coroutine that can resume on it.

The stdexec machinery lives in a pimpl defined in the translation unit, so this public header pulls in no stdexec headers — keeping them out of the C++20 module's global module fragment and off every downstream consumer that does not opt in.

Definition at line 25 of file ThreadPoolExecutor.hpp.

Constructor & Destructor Documentation

◆ ThreadPoolExecutor()

Lightweight::Async::ThreadPoolExecutor::ThreadPoolExecutor ( std::size_t  threadCount)
explicit

Constructs the pool and starts threadCount worker threads.

Parameters
threadCountNumber of worker threads to start (must be >= 1).
Exceptions
std::invalid_argumentif threadCount is 0 or exceeds the supported maximum (std::uint32_t, the width the underlying stdexec pool accepts).

◆ ~ThreadPoolExecutor()

Lightweight::Async::ThreadPoolExecutor::~ThreadPoolExecutor ( )
override

Waits for all in-flight work to drain, then stops and joins the worker threads.

Note
Must not be invoked from one of this pool's own worker threads (i.e. the pool must outlive every coroutine that can resume on it). The drain blocks the calling thread, so destroying the pool from a thread it owns would deadlock — the same constraint the previous join-based teardown had.

Member Function Documentation

◆ Post()

void Lightweight::Async::ThreadPoolExecutor::Post ( Work  work)
overridevirtual

Schedules work to run on the executor. Thread-safe.

Parameters
workThe work item to enqueue (consumed).

Implements Lightweight::Async::IExecutor.

◆ Resume()

void Lightweight::Async::ThreadPoolExecutor::Resume ( std::coroutine_handle<>  handle)
overridevirtual

Schedules handle to be resumed. Thread-safe.

Parameters
handleThe coroutine to resume.

Implements Lightweight::Async::IResumeScheduler.

◆ ThreadCount()

std::size_t Lightweight::Async::ThreadPoolExecutor::ThreadCount ( ) const
inlinenoexcept
Returns
the configured worker count (the value passed to the constructor).
Note
This is the requested count, not a live count of OS threads; it is fixed for the pool's lifetime and does not reflect any internal clamping the scheduler might apply.

Definition at line 54 of file ThreadPoolExecutor.hpp.


The documentation for this class was generated from the following file: