tmc::spawn_func()#
spawn_func() produces a wrapper that converts any functor (invocable) into an awaitable type. This wrapper exposes several customization functions that allow you to control where it runs and how the awaiting task is resumed.
It supports these Awaitable Customizations:
run_on(),
resume_on(),
with_priority(),
co_await,
fork(),
detach()
Usage Example#
// functions to wrap
void do_something();
SomeStruct process(std::vector<Data>& data);
std::vector<Data> data{};
// await a temporary inline (not very useful with spawn_func unless you customize it)
co_await tmc::spawn_func(do_something);
SomeStruct s = co_await tmc::spawn_func(process, data);
// apply customizations to control the execution behavior
co_await tmc::spawn_func(do_something).run_on(tmc::cpu_executor());
SomeStruct s = co_await tmc::spawn_func(process, data).with_priority(1);
// construct an explicit awaitable object (for composition)
tmc::aw_spawn_func<void> aw = tmc::spawn_func(do_something);
co_await std::move(aw);
tmc::aw_spawn_func<SomeStruct> aw = tmc::spawn_func(process, data);
SomeStruct s = co_await std::move(aw);
API Reference#
-
template<typename Func, typename ...Arguments>
auto tmc::spawn_func(Func &&func, Arguments&&... args) -> aw_spawn_func<decltype(func(args...))># Wraps a function into a new task by
std::binding the Func to its Args, and wrapping them into a type that allows you to customize the task behavior before submitting it for execution.Before the task is submitted for execution, you may call any or all of
run_on(),resume_on(),with_priority(). The task must then be submitted for execution by calling exactly one of:co_await,fork()ordetach().