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 (std::shared_ptr< detail::task_future< T > > future)
 
template<typename F>
auto then (F &&f) const
 
get () const
 
task_state get_state () const
 
std::exception_ptr get_exception () const
 
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
 
task_awaiter operator co_await () const
 

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 Function Documentation

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

If the task failed with an exception, rethrows the exception instead.

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

dispatch_queue::task<void> task my_coroutine() {
auto task = dispatch_queue.dispatch([]{ ... });
co_await task;
do_something_after_task_finished();
}
Definition dispatch_queue.hpp:13
task< Ret > dispatch(F &&f, Args &&... args)
Definition dispatch_queue.hpp:64
Definition task.hpp:22

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

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

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

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

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