class TaskScope
Defined at line 54 of file ../../sdk/lib/async_patterns/cpp/task_scope.h
|TaskScope| lets you post asynchronous tasks that are silently discarded when
the |TaskScope| object is destroyed. In contrast, tasks posted using
|async::PostTask|, |async::PostDelayedTask|, and so on will always run unless
the dispatcher is shutdown -- there is no separate mechanism to cancel them.
The task scope is usually a member of some bigger async business logic
object. By associating the lifetime of the posted async tasks with this
object, one can conveniently capture or borrow other members from the async
tasks without the need for reference counting to achieve memory safety.
Example:
class AsyncCounter {
public:
explicit AsyncCounter(async_dispatcher_t* dispatcher) : tasks_(dispatcher) {
// Post some tasks to asynchronously count upwards.
//
// It is okay if |AsyncCounter| is destroyed before some of these tasks
// come due. Those tasks will be discarded so they will not end up accessing
// a destroyed object.
tasks_.Post([this] { count_++ });
tasks_.PostDelayed([this] { count_++ }, zx::sec(1));
tasks_.PostDelayed(fit::bind_member
<
&AsyncCounter
::CheckCount>(this), zx::sec(5));
}
void CheckCount() {
assert(count_ == 2);
}
private:
TaskScope tasks_;
int count_ = 0;
};
|TaskScope| is thread-unsafe, and must be used and managed from a
[synchronized dispatcher][synchronized-dispatcher].
[synchronized-dispatcher]:
https://fuchsia.dev/fuchsia-src/development/languages/c-cpp/thread-safe-async#synchronized-dispatcher
Public Methods
void TaskScope (async_dispatcher_t * dispatcher)
Creates a |TaskScope| that will post all tasks to the provided |dispatcher|.
Defined at line 20 of file ../../sdk/lib/async_patterns/cpp/task_scope.cc
void ~TaskScope ()
Destroying the |TaskScope| synchronously destroys all pending tasks. If a
task attempts to reentrantly post more tasks into the |TaskScope| within
its destructor, those tasks will be synchronously destroyed too.
Defined at line 25 of file ../../sdk/lib/async_patterns/cpp/task_scope.cc
template <typename Closure>
require_nullary_fn<Closure> Post (Closure && handler)
Schedules to invoke |handler| with a deadline of now.
|handler| should be a |void()| callable object.
The handler will not run if |TaskScope| is destroyed before it comes due.
The handler will not run if the dispatcher shuts down before it comes due.
In both cases, the handler will be synchronously destroyed during task scope
destruction/dispatcher shutdown.
This is a drop-in replacement for |async::PostTask|.
Defined at line 80 of file ../../sdk/lib/async_patterns/cpp/task_scope.h
template <typename Closure>
require_nullary_fn<Closure> PostDelayed (Closure && handler, zx::duration delay)
Schedules to invoke |handler| with a deadline expressed as a |delay| from now.
|handler| should be a |void()| callable object.
The handler will not run if |TaskScope| is destroyed before it comes due.
The handler will not run if the dispatcher shuts down before it comes due.
In both cases, the handler will be synchronously destroyed during task scope
destruction/dispatcher shutdown.
This is a drop-in replacement for |async::PostDelayedTask|.
Defined at line 95 of file ../../sdk/lib/async_patterns/cpp/task_scope.h
template <typename Closure>
require_nullary_fn<Closure> PostForTime (Closure && handler, zx::time deadline)
Schedules to invoke |handler| with the specified |deadline|.
|handler| should be a |void()| callable object.
The handler will not run if |TaskScope| is destroyed before it comes due.
The handler will not run if the dispatcher shuts down before it comes due.
In both cases, the handler will be synchronously destroyed during task scope
destruction/dispatcher shutdown.
This is a drop-in replacement for |async::PostTaskForTime|.
Defined at line 110 of file ../../sdk/lib/async_patterns/cpp/task_scope.h