.. _spawn_func:

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 :ref:`Awaitable Customizations <awaitable_customizations>`:
:literal_ref:`run_on()<run_on>`,
:literal_ref:`resume_on()<resume_on>`,
:literal_ref:`with_priority()<with_priority>`,
:literal_ref:`co_await<co_await>`,
:literal_ref:`fork()<fork>`,
:literal_ref:`detach()<detach>`

Usage Example
-----------------------------------------------------------------------------------

.. code-block:: cpp

  // 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
-----------------------------------------------------------------------------------
.. doxygenfunction:: tmc::spawn_func(Func &&func, Arguments&&... args)
