ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
global_pool_backend.hpp
Go to the documentation of this file.
1#pragma once
2
12#include "worker_count.hpp"
13
14#include <cstddef>
15#include <mutex>
16#include <thread>
17#include <utility>
18
20{
21
22// ---------------------------------------------------------------------------
23// global_pool_backend
24// ---------------------------------------------------------------------------
25
53template <typename PoolType>
55{
56public:
63 static void
64 init(size_t num_threads)
65 {
66 std::call_once(init_flag(), [num_threads] { thread_count() = num_threads; });
67 }
68
70 static auto
71 instance() -> PoolType&
72 {
73 static PoolType pool(thread_count());
74 return pool;
75 }
76
80
81 template <typename F, typename... Args>
82 static auto
83 submit(F&& f, Args&&... args)
84 {
85 return instance().submit(std::forward<F>(f), std::forward<Args>(args)...);
86 }
87
88 template <typename F, typename... Args>
89 static auto
90 try_submit(F&& f, Args&&... args)
91 {
92 return instance().try_submit(std::forward<F>(f), std::forward<Args>(args)...);
93 }
94
95 template <typename F, typename... Args>
96 static void
97 post(F&& f, Args&&... args)
98 {
99 instance().post(std::forward<F>(f), std::forward<Args>(args)...);
100 }
101
102 template <typename F, typename... Args>
103 static auto
104 try_post(F&& f, Args&&... args)
105 {
106 return instance().try_post(std::forward<F>(f), std::forward<Args>(args)...);
107 }
108
109 template <typename Iterator>
110 static auto
111 submit_batch(Iterator begin, Iterator end)
112 {
113 return instance().submit_batch(begin, end);
114 }
115
116 template <typename Iterator>
117 static auto
118 try_submit_batch(Iterator begin, Iterator end)
119 {
120 return instance().try_submit_batch(begin, end);
121 }
122
123 template <typename Iterator, typename F>
124 static void
125 parallel_for_each(Iterator begin, Iterator end, F&& func)
126 {
127 instance().parallel_for_each(begin, end, std::forward<F>(func));
128 }
129
131
132private:
133 global_pool_backend() = default;
134
135 static auto
136 init_flag() -> std::once_flag&
137 {
138 static std::once_flag flag;
139 return flag;
140 }
141
142 static auto
143 thread_count() -> size_t&
144 {
145 static size_t count = default_worker_count();
146 return count;
147 }
148};
149
156
163
183template <typename Container, typename F>
184void
185parallel_for_each(Container& container, F&& func)
186{
187 global_thread_pool_backend::parallel_for_each(container.begin(), container.end(), std::forward<F>(func));
188}
189
190} // namespace threadschedule::detail
Singleton accessor for a process-wide pool instance.
static auto try_submit(F &&f, Args &&... args)
static void parallel_for_each(Iterator begin, Iterator end, F &&func)
static auto instance() -> PoolType &
Access the singleton pool instance (created on first call).
static auto try_post(F &&f, Args &&... args)
static void init(size_t num_threads)
Pre-configure the number of threads before first use.
static auto try_submit_batch(Iterator begin, Iterator end)
static auto submit_batch(Iterator begin, Iterator end)
static auto submit(F &&f, Args &&... args)
static void post(F &&f, Args &&... args)
Aggregates multiple thread_registry_backend instances into a single queryable view.
void parallel_for_each(Container &container, F &&func)
Convenience wrapper that applies a callable to every element of a container in parallel using the glo...
auto default_worker_count() noexcept -> std::size_t
Shared-queue pool implementation and public detail aliases.
Work-stealing deque and pool implementation.
Worker-thread count configuration for pool types.