![]() |
ImFusion C++ SDK 4.5.0
|
#include <ImFusion/Stream/TaskManager.h>
Manages asynchronous tasks with result storage using a thread pool. More...
Manages asynchronous tasks with result storage using a thread pool.
TaskManager provides a simple way to execute functions asynchronously and track their completion. Each task is assigned a unique TaskId that can be used to wait for completion or retrieve results. Tasks are executed on a fixed-size thread pool for efficient resource utilization.
Unlike Threading::ThreadPool, which returns raw std::future objects that the caller must manage, TaskManager retains ownership of results and provides a higher-level API for tracking completion status, waiting for individual or all tasks, and retrieving results by ID.
In addition to executing new tasks, TaskManager can also track existing std::future objects, allowing you to combine futures from different sources and wait for them together. Deferred futures (created with std::launch::deferred) are considered immediately ready (isReady() returns true) but are evaluated lazily — the actual computation runs when the result is requested via takeResult()/takeAllResults() or explicitly forced via waitAll()/waitFor().
Features:
Example usage running a single task many times:
More complex example with different types of tasks and futures:
Classes | |
| class | TaskId |
| Opaque identifier for a task managed by TaskManager. More... | |
Public Member Functions | |
| TaskManager (int numThreads=std::max< int >(1, static_cast< int >(std::thread::hardware_concurrency()))) | |
| Creates a TaskManager with a thread pool. | |
| TaskManager (const std::string threadNamePrefix) | |
| Creates a TaskManager with default number of threads. | |
| TaskManager (int numThreads, const std::string threadNamePrefix) | |
| Creates a TaskManager with given number of threads and specified prefix for thread names. | |
| template<typename Func, typename... Args> | |
| TaskId | addTask (Func &&func, Args &&... args) |
| Adds an async task to be executed on the thread pool. | |
| template<typename T> | |
| TaskId | addFuture (std::future< T > &&future) |
| Adds an existing future to be tracked by the TaskManager. | |
| bool | allReady () |
| Checks if all tasks are ready (completed or available for immediate evaluation). | |
| void | waitAll () |
| Blocks until all tasks have completed. | |
| bool | isReady (TaskId taskId) |
| Checks if a specific task is ready (completed or available for immediate evaluation). | |
| void | waitFor (TaskId taskId) |
| Blocks until the specified task has completed. | |
| template<typename T> | |
| std::optional< T > | takeResult (TaskId taskId) |
| Retrieves and removes the stored result for a completed task. | |
| template<typename T> | |
| std::vector< T > | takeAllResults (std::vector< TaskId > *outTaskIds=nullptr) |
| Retrieves and removes all stored results of the specified type from tasks that have already completed. | |
| int | numThreads () const |
| Returns the number of threads in the thread pool. | |
|
explicit |
Creates a TaskManager with a thread pool.
| numThreads | Number of worker threads. Defaults to hardware concurrency. |
|
explicit |
Creates a TaskManager with default number of threads.
| threadNamePrefix | Prefix for thread names in the thread pool ( |
|
inline |
Adds an async task to be executed on the thread pool.
Works for both void and non-void return types.
|
inline |
Adds an existing future to be tracked by the TaskManager.
Async futures are moved to finished once ready. Deferred futures are considered to be "ready-for-evaluation" and evaluated lazily on takeResult()/waitAll()/waitFor().
| bool ImFusion::TaskManager::allReady | ( | ) |
Checks if all tasks are ready (completed or available for immediate evaluation).
Deferred futures are considered ready but are not evaluated by this method.
| void ImFusion::TaskManager::waitAll | ( | ) |
Blocks until all tasks have completed.
Evaluates all deferred futures and waits for all async tasks to finish.
| bool ImFusion::TaskManager::isReady | ( | TaskId | taskId | ) |
Checks if a specific task is ready (completed or available for immediate evaluation).
Deferred futures are considered ready but are not evaluated by this method. Returns true for unknown task IDs (they are not running).
| void ImFusion::TaskManager::waitFor | ( | TaskId | taskId | ) |
Blocks until the specified task has completed.
Evaluates the deferred future if applicable. Returns immediately for unknown task IDs.
|
inline |
Retrieves and removes the stored result for a completed task.
Evaluates the deferred future if it has not been consumed yet. This transfers ownership of the result to the caller and frees the internal storage.
|
inline |
Retrieves and removes all stored results of the specified type from tasks that have already completed.
Evaluates deferred futures of matching type that have not been consumed yet. This transfers ownership of all matching results to the caller and frees the internal storage.
| outTaskIds | Output parameter that will be filled with the task IDs corresponding to each result. |