Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
Async.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Execution_Async_h_
5#define _Stroika_Foundation_Execution_Async_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <concepts>
10
11#include "Stroika/Foundation/Common/Common.h"
12
13/*
14 *
15 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
16 *
17 */
18
20
21 /**
22 * \brief Run all the argument functions (logically/potentially) in parallel, and wait until they all complete.
23 *
24 * Could be implemented with std::async, or ThreadPool.
25 *
26 * If any function throws, an arbitrary one of those exceptions will be rethrown by RunAll.
27 *
28 * All functions will complete before RunAll returns (regardless of whether any throw).
29 *
30 * \note no guarantee all run in parallel, but suggestion they are.
31 *
32 * This function returns the value of all completed functions as a tuple, unless they return void, in which case they are
33 * skipped, and for the special case of all returning void, the RunAll return type is void.
34 *
35 * \par Example Usage
36 * \code
37 * auto results = RunAll ([] () { return 1; }, [] () { return 2; }, [] () { return 3; });
38 * EXPECT_EQ (results, make_tuple (1, 2, 3));
39 * \endcode
40 *
41 * \par Example Usage
42 * \code
43 * tuple<int> results = RunAll ([] () -> void {}, [] () { return 3; });
44 * EXPECT_EQ (results, make_tuple (3));
45 * \endcode
46 *
47 * \par Example Usage
48 * \code
49 * int a = 0;
50 * int b = 1;
51 * int c = 2;
52 * RunAll ([&] () { a = 3; }, [&] () { b = 4; }, [&] () { c = 5; });
53 * EXPECT_EQ (a, 3);
54 * EXPECT_EQ (b, 4);
55 * EXPECT_EQ (c, 5);
56 * \endcode
57 *
58 * \par Example Usage
59 * \code
60 * static const auto kExcept_ = Execution::Exception {"Test exception"sv};
61 * auto thrower = [] () { Execution::Throw (kExcept_); };
62 * EXPECT_THROW (RunAll (thrower, [] () { return 3; }), Execution::Exception<>);
63 * \endcode
64 *
65 * \todo future versions of this function MAY cancel running functions if one throws.
66 *
67 * @todo Typically will auto-allocate threadpool of size #virtual CPUs (hardware_parallelism).
68 */
69 template <invocable<>... I>
70 auto RunAll (I... functions);
71
72}
73
74/*
75 ********************************************************************************
76 ***************************** Implementation Details ***************************
77 ********************************************************************************
78 */
79#include "Async.inl"
80
81#endif /*_Stroika_Foundation_Execution_Async_h_*/
auto RunAll(I... functions)
Run all the argument functions (logically/potentially) in parallel, and wait until they all complete.
Definition Async.inl:11