Github   Telegram
Loading...
Searching...
No Matches
Public Member Functions | List of all members
engine::TaskWithResult< T > Class Template Reference

#include <userver/engine/task/task_with_result.hpp>

+ Inheritance diagram for engine::TaskWithResult< T >:
+ Collaboration diagram for engine::TaskWithResult< T >:

Public Member Functions

 TaskWithResult ()=default
 Default constructor. More...
 
 TaskWithResult (TaskProcessor &task_processor, Task::Importance importance, Deadline deadline, std::unique_ptr< utils::impl::WrappedCall< T > > &&wrapped_call_ptr)
 Constructor, for internal use only. More...
 
 TaskWithResult (const TaskWithResult &)=delete
 
TaskWithResultoperator= (const TaskWithResult &)=delete
 
 TaskWithResult (TaskWithResult &&) noexcept=default
 
TaskWithResultoperator= (TaskWithResult &&) noexcept=default
 
Get () noexcept(false)
 Returns (or rethrows) the result of task invocation. After return from this method the task is not valid. More...
 
- Public Member Functions inherited from engine::Task
 Task ()
 Default constructor. More...
 
virtual ~Task ()
 Destructor. More...
 
 Task (Task &&) noexcept
 
Taskoperator= (Task &&) noexcept
 
bool IsValid () const
 Checks whether this Task object owns an actual task (not State::kInvalid) More...
 
State GetState () const
 Gets the task State.
 
bool IsFinished () const
 Returns whether the task finished execution.
 
void Wait () const noexcept(false)
 Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait(). More...
 
template<typename Rep , typename Period >
void WaitFor (const std::chrono::duration< Rep, Period > &) const noexcept(false)
 Suspends execution until the task finishes or after the specified timeout or until caller is cancelled. More...
 
template<typename Clock , typename Duration >
void WaitUntil (const std::chrono::time_point< Clock, Duration > &) const noexcept(false)
 Suspends execution until the task finishes or until the specified time point is reached or until caller is cancelled. More...
 
void WaitUntil (Deadline) const
 Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled. More...
 
void Detach () &&
 Detaches task, allowing it to continue execution out of scope; memory safety is much better with concurrent::BackgroundTaskStorage. More...
 
void RequestCancel ()
 Queues task cancellation request.
 
void SyncCancel () noexcept
 Cancels the task and suspends execution until it is finished. Can be called from coroutine context only. For non-coroutine context use RequestCancel() + BlockingWait().
 
TaskCancellationReason CancellationReason () const
 Gets task cancellation reason.
 
void BlockingWait () const
 

Additional Inherited Members

- Public Types inherited from engine::Task
enum class  Importance {
  kNormal ,
  kCritical
}
 Task importance. More...
 
enum class  State {
  kInvalid ,
  kNew ,
  kQueued ,
  kRunning ,
  kSuspended ,
  kCancelled ,
  kCompleted
}
 Task state. More...
 
enum class  WaitMode {
  kSingleWaiter ,
  kMultipleWaiters
}
 Task wait mode. More...
 
- Static Public Member Functions inherited from engine::Task
static const std::string & GetStateName (State state)
 

Detailed Description

template<typename T>
class engine::TaskWithResult< T >

Asynchronous task with result

Example usage:

std::vector<engine::TaskWithResult<int>> tasks;
tasks.reserve(container.size());
for (auto value : container) {
// Creating tasks that will be executed in parallel
tasks.push_back(utils::Async("some_task", [value = std::move(value)] {
engine::InterruptibleSleepFor(std::chrono::milliseconds(100));
return value;
}));
}
// we wait for the completion of tasks and get the results
std::vector<int> results;
results.reserve(tasks.size());
for (auto& task : tasks) {
// If the task completed with an exception,
// then Get () will throw an exception
results.push_back(task.Get());
}
See also
Synchronization Primitives
Examples
samples/flatbuf_service/flatbuf_service.cpp.

Definition at line 30 of file task_with_result.hpp.

Constructor & Destructor Documentation

◆ TaskWithResult() [1/2]

template<typename T >
engine::TaskWithResult< T >::TaskWithResult ( )
default

Default constructor.

Creates an invalid task.

◆ TaskWithResult() [2/2]

template<typename T >
engine::TaskWithResult< T >::TaskWithResult ( TaskProcessor &  task_processor,
Task::Importance  importance,
Deadline  deadline,
std::unique_ptr< utils::impl::WrappedCall< T > > &&  wrapped_call_ptr 
)
inline

Constructor, for internal use only.

Parameters
task_processortask processor used for execution of this task
importancespecifies whether this task can be auto-cancelled in case of task processor overload
wrapped_call_ptrtask body
See also
Async()

Definition at line 43 of file task_with_result.hpp.

Member Function Documentation

◆ Get()

template<typename T >
T engine::TaskWithResult< T >::Get ( )
inlinenoexcept

Returns (or rethrows) the result of task invocation. After return from this method the task is not valid.

Exceptions
WaitInterruptedExceptionwhen current_task::IsCancelRequested() and no TaskCancellationBlockers are present.
TaskCancelledExceptionif no result is available because the task was cancelled

Definition at line 62 of file task_with_result.hpp.


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