Implement async::executor::wait_all

This commit is contained in:
Nikita Lisitsa 2021-01-15 17:59:46 +03:00
parent e558799702
commit e0afe8b935

View file

@ -63,6 +63,8 @@ namespace psemek::async
template <typename T>
struct future
{
using result_type = T;
future() = default;
future(future&&) = default;
@ -191,6 +193,12 @@ namespace psemek::async
template <typename TimePoint, typename F, typename ... Args>
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<result_type> otherwise.
template <typename Iterator>
auto wait_all(Iterator begin, Iterator end);
virtual ~executor() {}
};
@ -230,4 +238,29 @@ namespace psemek::async
return future<R>(state);
}
template <typename Iterator>
auto executor::wait_all(Iterator begin, Iterator end)
{
using R = decltype(begin->get());
return dispatch([begin, end]{
if constexpr (std::is_same_v<R, void>)
{
for (auto it = begin; it != end; ++it)
{
it->get();
}
}
else
{
std::vector<R> results;
results.reserve(std::distance(begin, end));
for (auto it = begin; it != end; ++it)
{
results.push_back(it->get());
}
return results;
}
});
}
}