66 if (nthreads <= 1 || ntasks == 1) {
71 Index_ tasks_per_worker = ntasks / nthreads;
72 int remainder = ntasks % nthreads;
73 if (tasks_per_worker == 0) {
80 mexec.initialize(nthreads,
"failed to execute R command");
82 std::vector<std::thread> runners;
83 runners.reserve(nthreads);
84 std::vector<std::exception_ptr> errors(nthreads);
87 for (
int w = 0; w < nthreads; ++w) {
88 Index_ length = tasks_per_worker + (w < remainder);
90 runners.emplace_back([&](
int id, Index_ s, Index_ l) {
94 errors[id] = std::current_exception();
96 mexec.finish_thread();
103 for (
auto& x : runners) {
107 for (
const auto& err : errors) {
109 std::rethrow_exception(err);