Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
Async.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4
5#include <future>
6#include <vector>
7
9
10 template <invocable<>... I>
11 auto RunAll (I... functions)
12 {
13 constexpr bool kAllVoid_ = (is_void_v<invoke_result_t<I>> && ...);
14
15 tuple<future<invoke_result_t<I>>...> futures;
16
17 // Launch all async tasks
18 [&]<size_t... Idx> (index_sequence<Idx...>) { ((get<Idx> (futures) = async (launch::async, functions)), ...); }(index_sequence_for<I...>{});
19
20 // Collect and return results (get does wait and then throws if function threw)
21 if constexpr (kAllVoid_) {
22 // All functions return void - just wait for all to complete
23 [&]<size_t... Idx> (index_sequence<Idx...>) { (get<Idx> (futures).get (), ...); }(index_sequence_for<I...>{});
24 }
25 else {
26 // Mix of void and non-void, or all non-void - filter out void results
27 return [&]<size_t... Idx> (index_sequence<Idx...>) {
28 return tuple_cat ([&] () {
29 if constexpr (not is_void_v<invoke_result_t<tuple_element_t<Idx, tuple<I...>>>>) {
30 return make_tuple (get<Idx> (futures).get ());
31 }
32 else {
33 get<Idx> (futures).get (); // wait for completion, but skip result
34 return tuple<> ();
35 }
36 }()...);
37 }(index_sequence_for<I...>{});
38 }
39 }
40
41}
auto RunAll(I... functions)
Run all the argument functions (logically/potentially) in parallel, and wait until they all complete.
Definition Async.inl:11