|
morph 0.1.0
Asynchronous bridge between GUI thread and business models
|
Multi-threaded executor backed by a fixed-size thread pool. More...
#include <executor.hpp>
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. | |
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.
|
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.
| n | Number of threads to spawn. Values below 1 are clamped to 1. |
Definition at line 168 of file executor.hpp.
|
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.
|
inlineoverridevirtual |
Enqueues task for execution on one of the pool threads.
| task | Callable to execute. Thread-safe. |
Implements morph::exec::IExecutor.
Definition at line 202 of file executor.hpp.
|
inlinenodiscardoverridevirtualnoexcept |
Not serial: the workers run tasks at once.
Reimplemented from morph::exec::IExecutor.
Definition at line 210 of file executor.hpp.