Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
batch_operations.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
19#include <future>
20#include <vector>
21#include <utility>
22#include <type_traits>
23
24namespace kcenon::thread {
25
26namespace detail {
27
44template<typename Container, typename Operation>
45auto batch_apply(Container&& items, Operation&& op)
46{
47 using ResultType = decltype(op(std::move(*std::begin(items))));
48 std::vector<ResultType> results;
49 results.reserve(items.size());
50
51 for (auto&& item : items) {
52 results.push_back(op(std::move(item)));
53 }
54
55 return results;
56}
57
82template<typename T>
83auto collect_all(std::vector<std::future<T>>& futures) -> std::vector<T>
84{
85 std::vector<T> results;
86 results.reserve(futures.size());
87
88 for (auto& future : futures) {
89 results.push_back(future.get());
90 }
91
92 return results;
93}
94
103inline void collect_all(std::vector<std::future<void>>& futures)
104{
105 for (auto& future : futures) {
106 future.get();
107 }
108}
109
110} // namespace detail
111
112} // namespace kcenon::thread
auto collect_all(std::vector< std::future< T > > &futures) -> std::vector< T >
Collect all results from a vector of futures.
auto batch_apply(Container &&items, Operation &&op)
Apply an operation to each item in a collection, returning results.
Core threading foundation of the thread system library.
Definition thread_impl.h:17