PPL when_all with tasks of different types?

I would like to use the PPL "when_all" for tasks with different types. And add a β€œthen” call to this task.

But when_all returns a task that takes a vector, so all elements must be of the same type. So how do I do this?

This is what I came up with, but it feels a bit hacky:

//3 different types:
int out1;
float out2;
char out3;

//Unfortunately I cant use tasks with return values as the types would be  different ...
auto t1 = concurrency::create_task([&out1](){out1 = 1; }); //some expensive operation
auto t2 = concurrency::create_task([&out2](){out2 = 2; }); //some expensive operation
auto t3 = concurrency::create_task([&out3](){out3 = 3; }); //some expensive operation

auto t4 = (t1 && t2 && t3); //when_all doesnt block

auto t5 = t4.then([&out1, &out2, &out3](){
    std::string ret = "out1: " + std::to_string(out1) + ", out2: " + std::to_string(out2) + ", out3: " + std::to_string(out3);
    return ret;
});
auto str = t5.get();

std::cout << str << std::endl;

Has anyone got a better idea?

(parallel_invoke blocks, so I don't want to use this)

+4
source share
1 answer

Target groups will work.

Otherwise:

template<class...Ts>
struct get_many{
  std::tuple<task<Ts>...> tasks;
  template<class T0, size_t...Is>
  std::tuple<T0,Ts...>
  operator()(
    std::task<T0> t0,
    std::index_sequence<Is...>
  ){
    return std::make_tuple(
      t0.get(),
      std::get<Is>(tasks).get()...
    );
  }
  template<class T0>
  std::tuple<T0,Ts...>
  operator()(task<T0> t0){
    return (*this)(
      std::move(t0),
      std::index_sequence_for<Ts...>{}
    );
  }
};

template<class T0, class...Ts>
task<std::tuple<T0,Ts...>> when_every(
  task<T0> next, task<Ts>... later
){
  return next.then( get_many<Ts...>{
    std::make_tuple(std::move(later)...)
  } );
}

which does not work with tasks void, but otherwise associates any set of tasks with the task of tuples.

void - . - get_safe, T task<T> void_placeholder task<void>, . a partition_args, args task<void> task<T> - . . -append ( T tuple, void).

++ 14 (index_sequence index_sequence_for), ++ 11 ( 2-4 ), .

, , , . , . .

+2

All Articles