|
EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
|
A Worker provides an encapsulated thread-safe despatch model for submitting work and reviewing results. More...
#include <Worker.hpp>
Public Types | |
| using | ResultCallback = std::function<void()> |
| The type of callback to indicate new results. | |
Public Member Functions | |
| Worker (ResultCallback result_callback={}) | |
| Create a new Worker with an optional callback. | |
| void | submit (std::unique_ptr< ITask > &&task) |
| Submit some work to the scheduler for execution on the computation thread. | |
| bool | is_result_available () const |
| Checks the state of the result queue. | |
| std::optional< WorkerResult > | try_get_result () |
| Attempt to retrieve the latest WorkerResult object from the computation thread. | |
| void | clear () |
| Clears any scheduled jobs or pending results. | |
Private Member Functions | |
| void | execute (const std::stop_token &stop_token) noexcept |
| Executor running on the computation thread to receive work from the task queue and synchronously execute. | |
Private Attributes | |
| ThreadSafeQueue< std::unique_ptr< ITask > > | task_queue |
| ThreadSafeQueue< WorkerResult > | result_queue |
| ResultCallback | result_callback |
| Callable to inform clients of new results. | |
| std::jthread | worker_thread |
| RAII computation thread to handle ITask work pieces. | |
A Worker provides an encapsulated thread-safe despatch model for submitting work and reviewing results.
Clients may make use of a Worker to carry out thread-safe computation:
Clients may receive results of work by periodically polling the Worker (such as checking Worker::try_get_result in a game loop) or through the provided asynchronous callback invoked when a new WorkerResult is available. All Worker operations are atomic, so a single Worker instance may have clients on multiple threads.
Definition at line 44 of file Worker.hpp.
| using echomap::Worker::ResultCallback = std::function<void()> |
The type of callback to indicate new results.
Definition at line 47 of file Worker.hpp.
|
explicit |
Create a new Worker with an optional callback.
| result_callback | Client callback invoked to indicate new WorkerResult objects available for consumption. |
Definition at line 17 of file Worker.cpp.
| void echomap::Worker::clear | ( | ) |
Clears any scheduled jobs or pending results.
Definition at line 45 of file Worker.cpp.
|
privatenoexcept |
Executor running on the computation thread to receive work from the task queue and synchronously execute.
| stop_token | Cancellation token for the thread, provided by the thread interface. |
Definition at line 51 of file Worker.cpp.
|
nodiscard |
Checks the state of the result queue.
Definition at line 35 of file Worker.cpp.
| void echomap::Worker::submit | ( | std::unique_ptr< ITask > && | task | ) |
Submit some work to the scheduler for execution on the computation thread.
| task | A description of the task, detained within an owning container transferred to the Worker. |
Definition at line 27 of file Worker.cpp.
| std::optional< WorkerResult > echomap::Worker::try_get_result | ( | ) |
Attempt to retrieve the latest WorkerResult object from the computation thread.
This function does not block.
Definition at line 40 of file Worker.cpp.
|
private |
Callable to inform clients of new results.
Definition at line 95 of file Worker.hpp.
|
private |
Definition at line 93 of file Worker.hpp.
|
private |
Definition at line 92 of file Worker.hpp.
|
private |
RAII computation thread to handle ITask work pieces.
Definition at line 96 of file Worker.hpp.