ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
inline_pool.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../detail/callable/bind.hpp"
4#include "../detail/pool/inline_pool_backend.hpp"
5#include "../detail/pool/shutdown.hpp"
6#include "../detail/pool/worker_context_guard.hpp"
7#include "../detail/try_result.hpp"
8#include "../result.hpp"
9#include "../shutdown_policy.hpp"
10
11#include <cstddef>
12#include <future>
13#include <system_error>
14#include <type_traits>
15#include <utility>
16#include <vector>
17
19{
20class inline_pool final
21{
22public:
23 inline_pool() = default;
24
25 template <typename F, typename... Args>
26 auto
27 submit(F&& function, Args&&... args) -> result<std::future<detail::bind_result_t<F, Args...>>>
28 {
29 return impl_.try_submit(std::forward<F>(function), std::forward<Args>(args)...);
30 }
31
32 template <typename F, typename... Args>
33 auto
34 submit_or_throw(F&& function, Args&&... args) -> std::future<detail::bind_result_t<F, Args...>>
35 {
36 auto submitted = submit(std::forward<F>(function), std::forward<Args>(args)...);
37 if (!submitted)
38 throw std::system_error(submitted.error(), "inline_pool::submit");
39 return std::move(*submitted);
40 }
41
42 template <typename F, typename... Args>
43 auto
44 post(F&& function, Args&&... args) -> result<void>
45 {
46 return impl_.try_post(std::forward<F>(function), std::forward<Args>(args)...);
47 }
48
49 template <typename F, typename... Args>
50 void
51 post_or_throw(F&& function, Args&&... args)
52 {
53 auto posted = post(std::forward<F>(function), std::forward<Args>(args)...);
54 if (!posted)
55 throw std::system_error(posted.error(), "inline_pool::post");
56 }
57
58 template <typename Iterator>
59 auto
60 submit_batch(Iterator begin, Iterator end) -> result<std::vector<std::future<void>>>
61 {
62 return impl_.try_submit_batch(begin, end);
63 }
64
65 template <typename Iterator>
66 auto
67 submit_batch_or_throw(Iterator begin, Iterator end) -> std::vector<std::future<void>>
68 {
69 auto submitted = submit_batch(begin, end);
70 if (!submitted)
71 throw std::system_error(submitted.error(), "inline_pool::submit_batch");
72 return std::move(*submitted);
73 }
74
75 template <typename Iterator, typename Function>
76 auto
77 parallel_for_each(Iterator begin, Iterator end, Function&& function) -> result<void>
78 {
79 static_assert(detail::is_forward_iterator_v<Iterator>, "parallel_for_each requires at least a forward iterator");
80 return detail::try_result(
81 [&]() -> result<void>
82 {
83 impl_.parallel_for_each(begin, end, std::forward<Function>(function));
84 return {};
85 });
86 }
87
88 auto
90 {
91 impl_.wait_for_tasks();
92 return {};
93 }
94
95 auto
97 {
98 impl_.shutdown(detail::to_native(policy));
99 return {};
100 }
101
102 [[nodiscard]] auto
103 size() const noexcept -> std::size_t
104 {
105 return 0;
106 }
107
108 [[nodiscard]] auto
109 pending_tasks() const noexcept -> std::size_t
110 {
111 return 0;
112 }
113
114private:
116};
117} // namespace threadschedule::advanced
auto submit_batch_or_throw(Iterator begin, Iterator end) -> std::vector< std::future< void > >
auto shutdown(shutdown_policy policy=shutdown_policy::drain) -> result< void >
auto submit(F &&function, Args &&... args) -> result< std::future< detail::bind_result_t< F, Args... > > >
auto submit_or_throw(F &&function, Args &&... args) -> std::future< detail::bind_result_t< F, Args... > >
auto parallel_for_each(Iterator begin, Iterator end, Function &&function) -> result< void >
auto size() const noexcept -> std::size_t
auto submit_batch(Iterator begin, Iterator end) -> result< std::vector< std::future< void > > >
auto pending_tasks() const noexcept -> std::size_t
auto post(F &&function, Args &&... args) -> result< void >
void post_or_throw(F &&function, Args &&... args)
A pool that executes every task inline on the calling thread.
void shutdown(shutdown_policy_backend=shutdown_policy_backend::drain)
void parallel_for_each(Iterator begin, Iterator end, F &&func)
auto try_submit_batch(Iterator begin, Iterator end) -> expected< std::vector< std::future< void > >, std::error_code >
auto try_submit(F &&f, Args &&... args) -> expected< std::future< bind_result_t< F, Args... > >, std::error_code >
auto try_post(F &&f, Args &&... args) -> expected< void, std::error_code >
std::invoke_result_t< decltype(bind_args(std::declval< F >(), std::declval< Args >()...))& > bind_result_t
Result of invoking the decayed callable and arguments stored by bind_args.
Definition bind.hpp:32
constexpr auto to_native(shutdown_policy policy) noexcept -> shutdown_policy_backend
Definition shutdown.hpp:10
auto try_result(Function &&function) -> decltype(std::forward< Function >(function)())
shutdown_policy
Defines how pools handle pending work during shutdown.
@ drain
Finish queued work before returning from shutdown.