16 : task_pool_(worker_count) {}
23 size_t worker_count()
const noexcept {
24 return this->task_pool_.worker_count();
28 void parallel_for(
size_t task_count, Fn&& fn) {
29 if (task_count == 0) {
34 const size_t workers = this->task_pool_.worker_count();
35 if (workers <= 1 || task_count <= workers) {
36 this->run_serial(task_count, std::forward<Fn>(fn));
40 this->task_pool_.parallel_for(task_count, [&fn](
size_t,
size_t task_index) {
44 this->run_serial(task_count, std::forward<Fn>(fn));
50 void run_serial(
size_t task_count, Fn&& fn) {
51 for (
size_t i = 0; i < task_count; ++i) {
56 static size_t default_worker_count() {
58 const unsigned int hw = std::thread::hardware_concurrency();
59 return hw > 0 ?
static_cast<size_t>(hw) : 1;