94 Iterator first, Iterator last, Callback &&callback,
95 const std::size_t parallelism = std::thread::hardware_concurrency(),
96 const std::size_t stack_size_bytes = 0) ->
void {
97 const auto effective_parallelism{
98 std::forward_iterator<Iterator> ? (std::max)(parallelism, 1uz) : 1uz};
107 if (effective_parallelism == 1 || std::ranges::next(first) == last) {
108 std::size_t cursor{1};
109 for (
auto iterator = first; iterator != last; ++iterator, ++cursor) {
110 callback(*iterator, effective_parallelism, cursor);
116 std::queue<Iterator> tasks;
117 for (
auto iterator = first; iterator != last; ++iterator) {
118 tasks.push(iterator);
121 std::mutex queue_mutex;
122 std::mutex exception_mutex;
124 auto effective_callback = std::forward<Callback>(callback);
126 std::exception_ptr exception =
nullptr;
128 std::vector<std::thread> workers;
129 workers.reserve(effective_parallelism);
131 const auto total{tasks.size()};
136 auto worker_callable = [&tasks, &queue_mutex, &effective_callback, &exception,
138 worker_parallelism = effective_parallelism, total] {
144 std::unique_lock lock{queue_mutex};
149 const auto iterator{tasks.front()};
150 const auto cursor{total - tasks.size() + 1};
153 effective_callback(*iterator, worker_parallelism, cursor);
156 hold_first_exception(exception_mutex, exception,
157 std::current_exception());
161 std::optional<StaticString> creation_error;
166 for (std::size_t index = 0; index < effective_parallelism; ++index) {
167 auto *heap_function =
new std::function<void()>(worker_callable);
168 if (stack_size_bytes >
static_cast<std::size_t
>(UINT_MAX)) {
169 delete heap_function;
171 StaticString{
"The requested stack size is too large for this "
176 auto raw_handle = _beginthreadex(
177 nullptr,
static_cast<unsigned>(stack_size_bytes),
178 ¶llel_for_each_windows_thread_start, heap_function, 0,
nullptr);
179 if (raw_handle == 0) {
180 delete heap_function;
181 creation_error =
StaticString{
"Could not create thread"};
185 HANDLE thread_handle =
reinterpret_cast<HANDLE
>(raw_handle);
186 workers.emplace_back([thread_handle] {
187 WaitForSingleObject(thread_handle, INFINITE);
188 CloseHandle(thread_handle);
192 for (std::size_t index = 0; index < effective_parallelism; ++index) {
196 pthread_attr_init(&attr);
201 if (stack_size_bytes > 0 &&
202 pthread_attr_setstacksize(&attr, stack_size_bytes) != 0) {
203 pthread_attr_destroy(&attr);
205 StaticString{
"The requested stack size is not supported by this "
210 auto *heap_function =
new std::function<void()>(worker_callable);
211 pthread_t pthread_handle;
212 auto raw_handle = pthread_create(
213 &pthread_handle, &attr,
214 [](
void *arg) ->
void * {
215 auto *function_ptr =
static_cast<std::function<
void()
> *>(arg);
221 if (raw_handle != 0) {
222 pthread_attr_destroy(&attr);
223 delete heap_function;
224 creation_error =
StaticString{
"Could not create thread"};
227 workers.emplace_back(
228 [pthread_handle] { pthread_join(pthread_handle,
nullptr); });
229 pthread_attr_destroy(&attr);
238 if (creation_error.has_value()) {
239 std::scoped_lock lock{queue_mutex};
240 std::queue<Iterator> empty;
244 for (
auto &worker_thread : workers) {
245 worker_thread.join();
248 if (creation_error.has_value()) {
253 std::rethrow_exception(exception);