oneTBB icon indicating copy to clipboard operation
oneTBB copied to clipboard

Allow parallel_invoke to return a tuple.

Open BenFrantzDale opened this issue 1 year ago • 3 comments

I’ve gotten in the habit of using parallel_invoke like this:

int foo;
int bar;
tbb::parallel_invoke(
    [&] { foo = callFoo(); },
    [&] { bar = calcBar(); });

but this is noisy and means my types must be default-constructible. I want

auto [foo, bar] = tbb::parallel_invoke(calcFoo, calcBar);

This should be easy enough. It’s just the variadic version of

template <typename F0, Typename F1>
auto parallel_invoke(F0&& f0, F1&& f1) {
    std::tuple<std::optional<std::invoke_result_t<F0>>,
           std::optional<std::invoke_result_t<F0>>>> results;
    tbb::parallel_invoke(
        [&] { std::get<0>(results).emplace(std::invoke(FWD(f0))); },
        [&] { std::get<1>(results).emplace(std::invoke(FWD(f))); });
    return std::tuple{
        std::move(*std::get<0>(results)),
        std::move(*std::get<1>(results))};
}

I know this is doable. I just have to look it up. The only real trick is making it deal nicely with functions that return void. The way I’ve dealt with that in our codebase is to have a regular_void type that’s basically std::monostate.

BenFrantzDale avatar Mar 04 '23 01:03 BenFrantzDale