General Utility Library for C++14  2.12
Public Member Functions | List of all members
gul14::ThreadPool::TaskHandle< T > Class Template Reference

Detailed Description

template<typename T>
class gul14::ThreadPool::TaskHandle< T >

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.

auto pool = make_thread_pool(1);
auto task = pool->add_task([]() { return 42; });
while (not task.is_complete())
{
std::cout << "Waiting for task to complete...\n";
sleep(0.1);
}
std::cout << "Task result: " << task.get_result() << "\n";
std::shared_ptr< ThreadPool > make_thread_pool(std::size_t num_threads, std::size_t capacity=ThreadPool::default_capacity)
Create a thread pool with the desired number of threads and the specified capacity for queuing tasks.
Definition: ThreadPool.h:599
bool sleep(const std::chrono::duration< Rep, Period > &duration, const Trigger &trg)
Sleep for at least the given time span, with the option of being woken up from another thread.
Definition: time_util.h:114

#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...
 
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...
 

Constructor & Destructor Documentation

◆ TaskHandle() [1/2]

template<typename T >
gul14::ThreadPool::TaskHandle< T >::TaskHandle ( )
inline

Default-construct an invalid TaskHandle.

This constructor creates an invalid TaskHandle which has no result and is not associated with a ThreadPool.

◆ TaskHandle() [2/2]

template<typename T >
gul14::ThreadPool::TaskHandle< T >::TaskHandle ( TaskId  id,
std::future< T >  future,
std::shared_ptr< ThreadPool pool 
)
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.

Parameters
idUnique ID of the task
futureA std::future that will eventually contain the result of the task
poolA shared pointer to the ThreadPool that the task is associated with

Member Function Documentation

◆ cancel()

template<typename T >
bool gul14::ThreadPool::TaskHandle< T >::cancel ( )
inline

Remove the task from the queue if it is still pending.

This call has no effect if the task is already running.

Returns
true if the task was removed from the queue, false if it was not found in the queue (e.g. because it is already running).
Exceptions
std::logic_erroris thrown if the associated thread pool does not exist anymore.

◆ get_result()

template<typename T >
T gul14::ThreadPool::TaskHandle< T >::get_result ( )
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.

◆ get_state()

template<typename T >
TaskState gul14::ThreadPool::TaskHandle< T >::get_state ( ) const
inline

Determine if the task is running, waiting to be started, completed, or has been canceled.

Exceptions
std::logic_erroris thrown if the associated thread pool does not exist anymore.
Note
If you just need to find out if a task has finished running, prefer is_complete() over this function. It does not need to interact with the ThreadPool and is therefore slightly more performant.

◆ is_complete()

template<typename T >
bool gul14::ThreadPool::TaskHandle< T >::is_complete ( ) const
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.

Note
is_complete() does not need to interact with the ThreadPool to determine the state of the task. It is therefore slightly more performant than get_state(), but does not deliver the same fine-grained information.

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