tmc::spawn_func_many()#

spawn_func_many() wraps multiple functors into a single awaitable, that completes when all of the wrapped functors complete. The wrapped functors will be executed concurrently. Unlike spawn_func(), it requires that the provided functors take no arguments.

All overloads expect a forward iterator of functors, and an end iterator (sentinel), or element count. Some overloads support a Count or MaxCount template parameter. If you provide this parameter, any results will be returned in a std::array<Result, Count>. Otherwise, they will be returned in a std::vector<Result>.

An overload that directly accepts a range-type (which exposes .begin() and .end() member functions) is also provided.

If the Result type is not default-constructible, each value will be wrapped into a std::optional<Result>. Thus, the entire result type will be std::array<std::optional<Result>, Count> or std::vector<std::optional<Result>>.

The resulting awaitable supports these Awaitable Customizations: run_on(), resume_on(), with_priority(), co_await, fork(), detach()

Receiving Results As They Complete#

To receive each result as soon as it becomes ready - rather than waiting for the whole group to complete - use tmc::mux_many. It multiplexes a set of same-typed awaitables, returns the index of each one as it completes, and additionally lets you re-arm a completed slot with a new awaitable. mux_many replaces the former .result_each() execution mode.

API Reference#

template<size_t Count = 0, typename FuncIter, typename Functor = std::iter_value_t<FuncIter>, typename Result = std::invoke_result_t<Functor&>>
aw_spawn_many<Result, Count, FuncIter, size_t, true> tmc::spawn_func_many(FuncIter &&FunctorIterator)#

The single-argument form of spawn_func_many() has two overloads. If Count is non-zero (this overload), a fixed-size std::array<T, Count> will be allocated to return results in. The other overload (Count == 0) supports range-types.

Functor must be a copyable type that implements Result operator(). FuncIter must be an iterator type that implements operator*() and FuncIter& operator++().

Submits items in range [Begin, Begin + Count) to the executor.

Note: You must ensure the iterator remains in scope until this has been co_await ed. A temporary iterator is OK only if this is co_await ed immediately.

template<size_t Count = 0, typename FuncRange, typename FuncIter = tmc::detail::range_iter<FuncRange>::type, typename Functor = std::iter_value_t<FuncIter>, typename Result = std::invoke_result_t<Functor&>>
aw_spawn_many<Result, 0, FuncIter, FuncIter, true> tmc::spawn_func_many(FuncRange &&Range)#

The single-argument form of spawn_func_many() has two overloads. If Count is zero (this overload), the single argument is treated as a range. The other overload (Count != 0) supports fixed-size awaitable groups.

FuncRange must implement begin() and end() methods which return an iterator type. The iterator type must implement operator*(), FuncIter& operator++(), and operator==(FuncIter const& rhs).

Functor must be a copyable type that implements Result operator().

Submits items in range [Range.begin(), Range.end()) to the executor.

Note: You must ensure the range remains in scope until this has been co_await ed. A temporary range is OK only if this is co_await ed immediately.

template<typename FuncIter, typename Functor = std::iter_value_t<FuncIter>, typename Result = std::invoke_result_t<Functor&>>
aw_spawn_many<Result, 0, FuncIter, size_t, true> tmc::spawn_func_many(FuncIter &&FunctorIterator, size_t FunctorCount)#

For use when the number of items to spawn is a runtime parameter. Functor must be a copyable type that implements Result operator(). FuncIter must be an iterator type that implements operator*() and FuncIter& operator++(). FunctorCount must be non-zero.

Submits items in range [Begin, Begin + FunctorCount) to the executor.

Note: You must ensure the iterator remains in scope until this has been co_await ed. A temporary iterator is OK only if this is co_await ed immediately.

template<size_t MaxCount = 0, typename FuncIter, typename Functor = std::iter_value_t<FuncIter>, typename Result = std::invoke_result_t<Functor&>>
aw_spawn_many<Result, MaxCount, FuncIter, FuncIter, true> tmc::spawn_func_many(FuncIter &&Begin, FuncIter &&End)#

For use when the number of items to spawn may be variable. Functor must be a copyable type that implements Result operator(). FuncIter must be an iterator type that implements operator*(), FuncIter& operator++(), and operator==(FuncIter const& rhs).

  • If MaxCount is non-zero, the return type will be a std::array<Result, MaxCount>. Up to MaxCount tasks will be consumed from the iterator. If the iterator produces less than MaxCount tasks, elements in the return array beyond the number of results actually produced by the iterator will be default-initialized. Submits items in range [Begin, min(Begin + MaxCount, End)) to the executor.

  • If MaxCount is zero/not provided, the return type will be a right-sized std::vector<Result> with size and capacity equal to the number of tasks produced by the iterator. Submits items in range [Begin, End) to the executor.

Note: You must ensure the iterators remain in scope until this has been co_await ed.

template<typename FuncIter, typename Functor = std::iter_value_t<FuncIter>, typename Result = std::invoke_result_t<Functor&>>
aw_spawn_many<Result, 0, FuncIter, FuncIter, true> tmc::spawn_func_many(FuncIter &&Begin, FuncIter &&End, size_t MaxCount)#

For use when the number of items to spawn may be variable. Functor must be a copyable type that implements Result operator(). FuncIter must be an iterator type that implements operator*(), FuncIter& operator++(), and operator==(FuncIter const& rhs).

  • Up to MaxCount tasks will be consumed from the iterator.

  • The iterator may produce less than MaxCount tasks.

  • The return type will be a right-sized std::vector<Result> with size and capacity equal to the number of tasks consumed from the iterator.

Submits items in range [Begin, min(Begin + MaxCount, End)) to the executor.

Note: You must ensure the iterators remain in scope until this has been co_await ed.