morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::exec::ThreadPoolExecutor Class Reference

Multi-threaded executor backed by a fixed-size thread pool. More...

#include <executor.hpp>

Inheritance diagram for morph::exec::ThreadPoolExecutor:
morph::exec::IExecutor

Public Member Functions

 ThreadPoolExecutor (std::size_t n)
 Constructs the pool with n worker threads.
 ~ThreadPoolExecutor () override
 Signals all workers to stop and joins them.
void post (std::function< void()> task) override
 Enqueues task for execution on one of the pool threads.
bool isSerial () const noexcept override
 Not serial: the workers run tasks at once.
Public Member Functions inherited from morph::exec::IExecutor
 IExecutor (const IExecutor &)=delete
IExecutor & operator= (const IExecutor &)=delete
 IExecutor (IExecutor &&)=delete
IExecutor & operator= (IExecutor &&)=delete
::core::async::IExecutor & coreExecutor () noexcept
 This executor as a core-cpp executor: the identity an core::async::ExecutorScope names.

Detailed Description

Multi-threaded executor backed by a fixed-size thread pool.

Tasks are placed in a FIFO queue and consumed by worker threads. Exceptions thrown by tasks are caught and logged via morph::log (they do not propagate out of the worker or abort sibling tasks). The destructor blocks until all worker threads have exited.

Definition at line 159 of file executor.hpp.

Constructor & Destructor Documentation

◆ ThreadPoolExecutor()

morph::exec::ThreadPoolExecutor::ThreadPoolExecutor ( std::size_t n)
inlineexplicit

Constructs the pool with n worker threads.

n is clamped to a minimum of 1: a pool with zero workers would accept posted tasks that could never run (every post() would hang forever), so ThreadPoolExecutor(0) yields a usable single-worker pool rather than a silently dead one.

Parameters
nNumber of threads to spawn. Values below 1 are clamped to 1.

Definition at line 168 of file executor.hpp.

◆ ~ThreadPoolExecutor()

morph::exec::ThreadPoolExecutor::~ThreadPoolExecutor ( )
inlineoverride

Signals all workers to stop and joins them.

Workers drain the queue before exiting: after _stop is set they keep popping and running already-queued tasks and only return once the queue is empty, so the join blocks until every task queued before destruction has run. Tasks post()ed concurrently with or after destruction race this and may be silently lost.

Definition at line 182 of file executor.hpp.

Member Function Documentation

◆ post()

void morph::exec::ThreadPoolExecutor::post ( std::function< void()> task)
inlineoverridevirtual

Enqueues task for execution on one of the pool threads.

Parameters
taskCallable to execute. Thread-safe.

Implements morph::exec::IExecutor.

Definition at line 202 of file executor.hpp.

◆ isSerial()

bool morph::exec::ThreadPoolExecutor::isSerial ( ) const
inlinenodiscardoverridevirtualnoexcept

Not serial: the workers run tasks at once.

Returns
false.

Reimplemented from morph::exec::IExecutor.

Definition at line 210 of file executor.hpp.


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