General Utility Library for C++14
2.11
|
A handle for a task that has (or had) been enqueued on a ThreadPool.
A TaskHandle can be used to query the status of a task and to retrieve its result.
#include <ThreadPool.h>
Public Member Functions | |
TaskHandle () | |
Default-construct an invalid TaskHandle. More... | |
TaskHandle (TaskId id, std::future< T > future, std::shared_ptr< ThreadPool > pool) | |
Construct a TaskHandle. More... | |
bool | cancel () |
Remove the task from the queue if it is still pending. More... | |
T | get_result () |
Block until the task has finished and return its result. More... | |
bool | is_complete () const |
Determine whether the task has completed. More... | |
TaskState | get_state () const |
Determine if the task is running, waiting to be started, completed, or has been canceled. More... | |
|
inline |
Default-construct an invalid TaskHandle.
This constructor creates an invalid TaskHandle which has no result and is not associated with a ThreadPool.
|
inline |
Construct a TaskHandle.
This constructor is not meant to be used directly. Instead, TaskHandles are returned by the ThreadPool when a task is enqueued.
id | Unique ID of the task |
future | A std::future that will eventually contain the result of the task |
pool | A shared pointer to the ThreadPool that the task is associated with |
|
inline |
Remove the task from the queue if it is still pending.
This call has no effect if the task is already running.
std::logic_error | is thrown if the associated thread pool does not exist anymore. |
|
inline |
Block until the task has finished and return its result.
If is_complete() == true
, the result is available immediately. If the task finished by throwing an exception, get_result() rethrows this exception.
|
inline |
Determine if the task is running, waiting to be started, completed, or has been canceled.
std::logic_error | is thrown if the associated thread pool does not exist anymore. |
|
inline |
Determine whether the task has completed.
This function returns true if the task has finished, either successfully or by throwing an exception. It returns false if the task is still running, waiting to be started, or has been canceled.