Dispatch Queue
Dispatch Queue / Thread Pool implementation for C++11 with built-in C++20 coroutine support
 
Loading...
Searching...
No Matches
dispatch_queue::task< T > Class Template Reference

#include <task.hpp>

Public Types

using value_type = T
 

Public Member Functions

 task ()=default
 
 task (std::shared_ptr< detail::task_future< T > > future)
 
bool valid () const
 
template<typename F>
auto then (F &&f) const
 
get () const
 
template<typename U = T, typename = typename std::enable_if<std::is_void<U>::value>::type>
void set_value ()
 
template<typename U = T, typename = typename std::enable_if<not std::is_void<U>::value>::type>
void set_value (U &&value)
 
task_state get_state () const
 
std::exception_ptr get_exception () const
 
void set_exception (std::exception_ptr exception)
 
void wait () const
 
template<class Rep, class Period>
bool wait_for (const std::chrono::duration< Rep, Period > &timeout_duration) const
 
template<class Clock, class Duration>
bool wait_until (const std::chrono::time_point< Clock, Duration > &timeout_time) const
 
 operator task< void > () const
 
template<typename U, typename = typename std::enable_if<std::is_convertible<T, U>::value>::type>
 operator task< U > () const
 
task_awaiter operator co_await () const
 

Static Public Member Functions

static task create_pending ()
 
template<typename U = T, typename = typename std::enable_if<std::is_void<U>::value>::type>
static task create_ready ()
 
template<typename U = T, typename = typename std::enable_if<not std::is_void<U>::value>::type>
static task create_ready (U &&value)
 
static task create_failed (std::exception_ptr exception)
 

Detailed Description

template<typename T>
class dispatch_queue::task< T >

This template class represents asynchronous tasks that run in dispatch queues.

Similar to std::shared_future, but with the addition of support for continuations (then), checking for task state (get_state) and built-in C++20 coroutine support (operator co_await).

All methods are thread-safe.

Member Typedef Documentation

◆ value_type

template<typename T>
using dispatch_queue::task< T >::value_type = T

Constructor & Destructor Documentation

◆ task() [1/2]

template<typename T>
dispatch_queue::task< T >::task ( )
default

Default-constructed tasks are invalid and don't hold any shared state.

◆ task() [2/2]

template<typename T>
dispatch_queue::task< T >::task ( std::shared_ptr< detail::task_future< T > > future)
inline

Member Function Documentation

◆ create_failed()

template<typename T>
static task dispatch_queue::task< T >::create_failed ( std::exception_ptr exception)
inlinestatic

Creates a failed task<T> with the passed exception.

◆ create_pending()

template<typename T>
static task dispatch_queue::task< T >::create_pending ( )
inlinestatic

Creates a pending task.

◆ create_ready() [1/2]

template<typename T>
template<typename U = T, typename = typename std::enable_if<std::is_void<U>::value>::type>
static task dispatch_queue::task< T >::create_ready ( )
inlinestatic

Creates a ready task<void>.

◆ create_ready() [2/2]

template<typename T>
template<typename U = T, typename = typename std::enable_if<not std::is_void<U>::value>::type>
static task dispatch_queue::task< T >::create_ready ( U && value)
inlinestatic

Creates a ready task<T> with the passed value.

◆ get()

template<typename T>
T dispatch_queue::task< T >::get ( ) const
inline

Waits until the task's value is ready (by calling wait), then returns the stored value.

Exceptions
...If the task failed with an exception, rethrows the exception instead.
task_errorThrown when the task is invalid (get_state() == task_state::invalid).

◆ get_exception()

template<typename T>
std::exception_ptr dispatch_queue::task< T >::get_exception ( ) const
inline

Returns the exception thrown while running task, if there's any.

◆ get_state()

template<typename T>
task_state dispatch_queue::task< T >::get_state ( ) const
inline

Returns the task state.

◆ operator co_await()

template<typename T>
task_awaiter dispatch_queue::task< T >::operator co_await ( ) const
inline

Returns an awaiter that resumes coroutines on the task's continuation.

auto task = dispatch_queue.dispatch([]{ ... });
co_await task;
do_something_after_task_finished();
}
Definition task.hpp:32
Definition dispatch_queue.hpp:19

◆ operator task< U >()

template<typename T>
template<typename U, typename = typename std::enable_if<std::is_convertible<T, U>::value>::type>
dispatch_queue::task< T >::operator task< U > ( ) const
inlineexplicit

Convert to task of convertible type.

◆ operator task< void >()

template<typename T>
dispatch_queue::task< T >::operator task< void > ( ) const
inline

Convert valued task to void task.

◆ set_exception()

template<typename T>
void dispatch_queue::task< T >::set_exception ( std::exception_ptr exception)
inline

Mark a pending task as failed with the specified exception.

Exceptions
task_errorThrown if task is not pending.

◆ set_value() [1/2]

template<typename T>
template<typename U = T, typename = typename std::enable_if<std::is_void<U>::value>::type>
void dispatch_queue::task< T >::set_value ( )
inline

Mark a pending task as ready.

Exceptions
task_errorThrown if task is not pending.

◆ set_value() [2/2]

template<typename T>
template<typename U = T, typename = typename std::enable_if<not std::is_void<U>::value>::type>
void dispatch_queue::task< T >::set_value ( U && value)
inline

Mark a pending task as ready with the specified value.

Exceptions
task_errorThrown if task is not pending.

◆ then()

template<typename T>
template<typename F>
auto dispatch_queue::task< T >::then ( F && f) const
inline

Add a continuation f that is guaranteed to run after this task finishes.

If the task is not finished yet, f will run right after the task finishes in the same thread where the task ran. Otherwise, f will run immediately in the calling thread.

Exceptions
task_errorThrown when the task is invalid (get_state() == task_state::invalid)

◆ valid()

template<typename T>
bool dispatch_queue::task< T >::valid ( ) const
inline

Checks if the task refers to a shared state.

◆ wait()

template<typename T>
void dispatch_queue::task< T >::wait ( ) const
inline

Waits until the task is either ready or failed with an exception.

If the task is pending (get_state() == task_state::pending), blocks indefinitely until task finishes. Otherwise returns immediately without blocking.

Exceptions
task_errorThrown when the task is invalid (get_state() == task_state::invalid)

◆ wait_for()

template<typename T>
template<class Rep, class Period>
bool dispatch_queue::task< T >::wait_for ( const std::chrono::duration< Rep, Period > & timeout_duration) const
inline

Waits for at most timeout_duration until the task is either ready or failed with an exception.

If the task is pending (get_state() == task_state::pending), blocks until task finishes or until the specified timeout_duration has elapsed. Otherwise returns immediately without blocking.

Returns
true if the task is finished, otherwise false.
Exceptions
task_errorThrown when the task is invalid (get_state() == task_state::invalid)

◆ wait_until()

template<typename T>
template<class Clock, class Duration>
bool dispatch_queue::task< T >::wait_until ( const std::chrono::time_point< Clock, Duration > & timeout_time) const
inline

Waits until timeout_time has been reached or until the task is either ready or failed with an exception, whichever comes first.

If the task is pending (get_state() == task_state::pending), blocks until task finishes or until the specified timeout_time has been reached. Otherwise returns immediately without blocking.

Returns
true if the task is finished, otherwise false.
Exceptions
task_errorThrown when the task is invalid (get_state() == task_state::invalid)

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