15 if (
count == 0)
return 0;
16 const auto limit = requested > 0 ?
static_cast<unsigned>(requested) : 8u;
17 const auto workers = std::min({
count,
static_cast<std::size_t
>(
limit),
18 static_cast<std::size_t
>(std::max(1u, std::thread::hardware_concurrency()))});
20 std::vector<std::exception_ptr> errors(
count);
21 std::atomic<std::size_t> next{0};
24 const auto index = next.fetch_add(1, std::memory_order_relaxed);
30 errors[
index] = std::current_exception();
34 std::vector<std::thread> threads;
35 threads.reserve(workers - 1);
37 for (std::size_t i = 1; i < workers; ++i) threads.emplace_back(
run);
39 for (
auto& thread : threads) thread.join();
44 for (
auto& thread : threads) thread.join();
46 for (
const auto&
error : errors)
48 return static_cast<int>(workers);