From e0afe8b935cbbb48a7d444bdd15044e79472284f Mon Sep 17 00:00:00 2001 From: lisyarus Date: Fri, 15 Jan 2021 17:59:46 +0300 Subject: [PATCH] Implement async::executor::wait_all --- libs/async/include/psemek/async/executor.hpp | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/libs/async/include/psemek/async/executor.hpp b/libs/async/include/psemek/async/executor.hpp index 01167acd..9eb3e6f9 100644 --- a/libs/async/include/psemek/async/executor.hpp +++ b/libs/async/include/psemek/async/executor.hpp @@ -63,6 +63,8 @@ namespace psemek::async template struct future { + using result_type = T; + future() = default; future(future&&) = default; @@ -191,6 +193,12 @@ namespace psemek::async template auto dispatch_at(TimePoint time, auto_cancel_tag, F && f, Args && ... args); + // Create a future that waits for all the provided futures. + // The returned future's result_type is void if the argument + // futures' result_type is void, and vector otherwise. + template + auto wait_all(Iterator begin, Iterator end); + virtual ~executor() {} }; @@ -230,4 +238,29 @@ namespace psemek::async return future(state); } + template + auto executor::wait_all(Iterator begin, Iterator end) + { + using R = decltype(begin->get()); + return dispatch([begin, end]{ + if constexpr (std::is_same_v) + { + for (auto it = begin; it != end; ++it) + { + it->get(); + } + } + else + { + std::vector results; + results.reserve(std::distance(begin, end)); + for (auto it = begin; it != end; ++it) + { + results.push_back(it->get()); + } + return results; + } + }); + } + }