This project is read-only.

when_any

defined in header <pplpp.h>
template <typename ... _Ty>
concurrency::task<std::tuple<_Ty...>> when_any(_Ty... tasks)

Summary

Creates a task that will complete successfully when any of the tasks supplied as arguments completes successfully.

Parameters

  • tasks : A list of heterogeneous tasks.

Return values

A task that completes successfully when any of the input tasks have completed successfully.
If the input tasks are task<T1> and task<T2>, the output of this function is task<std::tuple<task<T1>, task<T2>>>

Sample

#include "pplpp.h"
concurrency::task<void> t1([]{
      throw std::runtime_error("Test Exception");
});
concurrency::task<int> t2([]{
      int pn = 0;
       for (int i = 0; i < 1000000; ++i)
       {
           pn++;
       }
       return pn; 
});
pplpp::when_any(t1,t2)
.then([=](std::tuple<concurrency::task<void>,concurrency::task<int>> tuple){
    pplpp::when_all(t).then([](std::tuple<concurrency::task<void>, concurrency::task<int>> _t){
         try
        {
            std::get<0>(_t).get();
            std::get<1>(_t).get();
        }catch(...)
        {
        }
    });
});

Last edited Jul 8, 2013 at 10:42 PM by gbb21, version 3

Comments

petke Jan 25, 2016 at 7:59 PM 
It would be good to get a index to which task in the tuple finished. I guess one has to loop through all tasks and check for is_done() to figure out which one finished.

jstanard Jul 22, 2013 at 10:16 PM 
I think you meant to name the first tuple argument "t" rather than "tuple". Otherwise, I'm not sure what "when_all(t)" refers to.