1

I have a std::vector< concurrency::task<void> > with a list of tasks that may or may not load in order. I came across the concurrency::when_any call, but don't know enough information about how to use it.

I came across this microsoft sample that uses the call (ScenarioInput1.xaml.cpp:100), but I don't understand the pair parameter, and how to adapt that to my return values.

Edit: What I'm trying to do:

struct TaskInfo {
  unsigned int                    uniqueId;
  concurrency::task<unsigned int> task;
};
typedef std::vector< TaskInfo > TaskList;
typedef std::vector< unsigned int > TaskReturns;


// Somewhere inside a class, tasks and finished are member variable.
TaskList tasks;
bool finished = false;

// Inside a member function of the same class as above
// Populate the tasks

concurrency::when_any( tasks.begin(), tasks.end() ).then([this](std::vector<unsigned int> ids){
  for( std::vector<unsigned int>::iterator list=ids.begin; list != ids.end(); ++ids ) {
    // a mutex lock would happen here
    for( TaskList::iterator allTasks = tasks.begin(); allTasks != tasks.end(); ++allTasks ) {
      if ( *list == allTasks->uniqueId ) { tasks.erase( allTasks ); break; }
    }
    // mutex unlock
    if ( tasks.size() == 0 ) { finished = true; break }
  }
}

// In another member function
if ( finished ) doOtherThings();

If what I'm trying to do is not as well thought out or not as efficient, please let me know.

6
  • 1
  • I'm confused, I don't see any usage of when_any() in the file you referenced.
    – svick
    Feb 15, 2013 at 1:34
  • Sorry, it seems the download package and the online source code are slightly different.
    – OzBarry
    Feb 15, 2013 at 13:36
  • Ah, sorted it out, thanks to @hans-passant - the parameter in when_any( ... ).then( <here> ) should be a std::pair< returnType, size_t indexOfTask>. @hans-passant, please add an answer and I will mark it. Thanks for the link.
    – OzBarry
    Feb 15, 2013 at 14:44
  • Looks to me you can write a much better answer than I could. So go ahead and post one and mark it as the answer. Feb 15, 2013 at 14:46

2 Answers 2

4

It's the "choice" combination for tasks. You have a continuation (then), a join (when_all) and choice (when_any). The docs say ...

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

See the task class and Asynchronous programming in C++ for more details. More discussion and examples.

Take for example:

task<string> tasks[] = {t1, t2, t3};
auto taskResult =  when_all (begin(tasks), end(tasks))
    .then([](std::vector<string> results) {

... is equivalent to ...

(t1 && t2 && t3).then

Whereas ...

task<string> tasks[] = {t1, t2, t3};
auto taskResult = when_any (begin(tasks), end(tasks))
    .then([](string result) {

... is equivalent to ...

(t1 || t2 || t3).then
1
  • After doing some more reading, it looks like rather than (from your example) string result as the when_any parameter for the .then action, it should be std::pair<string, size_t> result. At least I wasn't the only confused one :)
    – OzBarry
    Feb 15, 2013 at 15:55
1

Hans Passant provided this link which told me that when_any( ... ).then([]( <params> ) requires to be of the type std::pair<returnType, size_t index>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.