Implement async::executor::wait_all
This commit is contained in:
parent
e558799702
commit
e0afe8b935
1 changed files with 33 additions and 0 deletions
|
|
@ -63,6 +63,8 @@ namespace psemek::async
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct future
|
struct future
|
||||||
{
|
{
|
||||||
|
using result_type = T;
|
||||||
|
|
||||||
future() = default;
|
future() = default;
|
||||||
future(future&&) = default;
|
future(future&&) = default;
|
||||||
|
|
||||||
|
|
@ -191,6 +193,12 @@ namespace psemek::async
|
||||||
template <typename TimePoint, typename F, typename ... Args>
|
template <typename TimePoint, typename F, typename ... Args>
|
||||||
auto dispatch_at(TimePoint time, auto_cancel_tag, F && f, Args && ... 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() {}
|
virtual ~executor() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -230,4 +238,29 @@ namespace psemek::async
|
||||||
return future<R>(state);
|
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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue