Represents an allocated, but not‐started coroutine, which is not yet been bound to an executor.
Synopsis
Declared in <folly/coro/Task.h>
template<typename T = void>
class [[nodiscard]] Task;
Description
You can only co_await a Task from within another Task, in which case it is implicitly bound to the same executor as the parent Task.
Alternatively, you can explicitly provide an executor by calling co_withExecutor(executor, task()), which will return a not‐yet‐started TaskWithExecutor that can be `co_await`ed anywhere and that will automatically schedule the coroutine to start executing on the bound executor when it is `co_await`ed.
Within the body of a Task's coroutine, executor binding to the parent executor is maintained by implicitly transforming all 'co_await expr' expressions into `co_await co_viaIfAsync(parentExecutor, expr)' to ensure that the coroutine always resumes on the parent's executor.
The Task coroutine is RequestContext‐aware and will capture the current RequestContext at the time the coroutine function is either awaited or explicitly started and will save/restore the current RequestContext whenever the coroutine suspends and resumes at a co_await expression.
More documentation on how to use coroutines is available at https://github.com/facebook/folly/blob/main/folly/coro/README.md
refcode folly/docs/examples/folly/coro/Task.cpp
Type Aliases
Name |
Description |
Awaiter type exposed for use by tests. |
|
The storage type used to hold the task's result. |
|
Marks |
|
The coroutine promise type backing this task. |
Member Functions
Name |
Description |
|
Constructors |
|
Destructor |
Move‐assigns from another task. |
|
|
Legacy way to bind this task to an executor. |
Converts a Task into a SemiFuture object. |
|
Swaps the coroutine handles of two tasks. |
Friends
Name |
Description |
Represents an allocated but not yet started coroutine that has already been bound to an executor. |
|
Invokes a callable and wraps its awaited result in a Task. |
|
Attaches a cancellation token to the task. |
|
Adapts the task to run on the given executor when awaited. |
|
Specify the executor that this task should execute on: co_withExecutor(executor, std::move(task)) |
Non-Member Functions
Name |
Description |
Accumulate the values from an input stream using a binary operation. |
|
Accumulate the values from an input stream into a single value, similar to |
|
Async version of the folly::transition_lock TODO: add more transition policies beyond just from upgrade to exclusive |
|
Concurrently await a range of value‐less awaitables. |
|
Concurrently await a range of awaitables and collect their results. |
|
Concurrently await a range of awaitables and collect a vector of Try results. |
|
Await a range of awaitables with bounded concurrency, collecting Try results. |
|
Await a range of awaitables with bounded concurrency, collecting results. |
|
Await a range of value‐less awaitables with bounded concurrency. |
|
Concurrently await awaitables and complete with the first result and index. |
|
Concurrently await awaitables and return every result once one finishes. |
|
Await a range of awaitables and return every result once one finishes. |
|
Concurrently await a range of awaitables and return the first result and index. |
|
Concurrently await awaitables, returning the first success or last error. |
|
Await a range of awaitables, returning the first success or last error. |
|
detachOnCancel is used to handle operations that are hard to be cancelled. A typical use case is: The caller starts a task with timeout (in this case, 1 sec timeout). The task itself launches a long running job and the job doesn't handle cancellation (sleep_for in this example). The caller has timeout and the cancellation is propagated to the task. The detachOnCancel detects the cancellation and return immediately. However, the background task still runs until the thread join. |
|
Make a Task that will trivially yield an Exception. |
|
Make a Task out of a Try. |
|
Make a Task that trivially returns with no return value. |
|
Same as makeTask(). See Unit |
|
Make a task that trivially returns a value. |
|
Execute a given asynchronous operation returned by func(), retrying it on failure, if desired, after awaiting retryDelay(error). |
|
Return a task that, when awaited, will sleep for the specified duration. |
|
Return a task that, when awaited, will sleep for the specified duration. |
|
Awaits |
|
Wrap a SemiFuture<Unit> in a Task without starting it. |
|
Wrap a SemiAwaitable in a Task without starting it. |
|
Wrap a referenced SemiAwaitable in a Task without starting it. |
|
Wrap a Future<Unit> in a Task without starting it. |
|
Converts a Future to a Task that cancels the future on cancellation. |
|
Converts a SemiFuture to a Task that cancels the future on cancellation. |
|
Converts a |
|
Converts a |
|
|
Block on a task's execution. Should be called from an Async annotated function. The fiber executing task_wait will block while the task is suspended, and the task's work will be executed inline on the fiber main context. |
|
Block on a void task's execution from an Async annotated function. |
Return Value
|
Note
|
The return value should not be discarded. |
Created with MrDocs