userver: engine::Task Class Reference
Loading...
Searching...
No Matches
engine::Task Class Reference

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

Detailed Description

Asynchronous task that has a unique ownership of the payload.

See engine::TaskWithResult for a type that could return a value or report an exception from the payload.

Definition at line 16 of file task.hpp.

+ Inheritance diagram for engine::Task:
+ Collaboration diagram for engine::Task:

Public Member Functions

 Task ()
 Default constructor.
 
 ~Task ()
 If the task is still valid and is not finished, cancels it and waits until it finishes.
 
 Task (Task &&other) noexcept
 Moves the other task into this, leaving the other in an invalid state.
 
Taskoperator= (Task &&other) noexcept
 If this Task is still valid and is not finished, cancels it and waits until it finishes before moving the other. Otherwise just moves the other task into this, leaving the other in invalid state.
 
 Task (const Task &)=delete
 
Taskoperator= (const Task &)=delete
 
void Detach () &&
 Detaches task, allowing it to continue execution out of scope; memory safety is much better with concurrent::BackgroundTaskStorage.
 
- Public Member Functions inherited from engine::TaskBase
bool IsValid () const
 Checks whether this object owns an actual task (not State::kInvalid)
 
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().
 
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.
 
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.
 
void WaitUntil (Deadline) const
 Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled.
 
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::TaskBase
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::TaskBase
static const std::string & GetStateName (State state)
 

Constructor & Destructor Documentation

◆ Task()

engine::Task::Task ( )

Default constructor.

Creates an invalid task.

Member Function Documentation

◆ Detach()

void engine::Task::Detach ( ) &&

Detaches task, allowing it to continue execution out of scope; memory safety is much better with concurrent::BackgroundTaskStorage.

Note
After detach, Task becomes invalid
Warning
Variables, which are captured by reference for this task in Async*, should outlive the task execution. This is hard to achieve in general, detached tasks may outlive all the components! Use concurrent::BackgroundTaskStorage as a safe and efficient alternative to calling Detach().

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