Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create concurrency::task from result?

Tags:

c++

c++11

task

ppl

I want to create a new task that is already completed from the given result. My current workaround is:

return concurrency::task<T>([]{return result;});

Is there anything better?

The problem is with the following code:

concurrency::task<bool> foo()
{
    if (smth)
        // the result is 
        return (foo_other() && foo_other2()).then([](std::vector<bool> results) {...});

    //return false;
    return concurrency::task<bool>([]{return false;});
}

As you can see, my function is asynchronous and it depends on another asynchronous function. But sometimes I can exit the app e.g. if the async task was already done. In that case I need to return a continuable task which returns a specified result.

like image 498
Toni Petrina Avatar asked Sep 03 '25 03:09

Toni Petrina


1 Answers

You can create a completed task as follows:

concurrency::task_from_result<bool>(false);
like image 91
Pawel Avatar answered Sep 04 '25 18:09

Pawel