Modern asynchronous C++ API design
// create task
pplx::task<int> lIntTask1([]
{
return 1;
});
auto lIntTask2 = pplx::create_task([]
{
return 2;
});To check if the task is finished or not, we can use 'bool pplx::task<T>::is_done()'.
To block until the task finishes, use 'void pplx::task<T>::wait()'.
To block until the task finishes and fetch the result of the task, use 'T pplx::task<T>::get()'.
if (lIntTask1.is_done())
{
cout << lIntTask1.get() << endl;
}
lIntTask2.wait();
cout << lIntTask2.get() << endl;You can use the task::then to compose tasks into a set of dependent operations. This composition model is supported by the notion of continuations.
// create task continuation
pplx::task<void> lIntContinuation1 = lIntTask1.then([](int iNumber)
{
cout << iNumber << endl;
});
auto lIntContinuation2 = lIntTask2.then([](pplx::task<int> iTask)
{
cout << iTask.get() << endl;
});cout << "running a task..." << endl;
auto t = pplx::create_task([]() -> int
{
throw exception();
return 1;
});
// value-based continuation.
auto c1 = t.then([](int n)
{
// we don't expect to get here because the antecedent task always throws.
cout << "received " << n << "from C1" << endl;
});
// Task-based continuation.
auto c2 = t.then([](pplx::task<int> previousTask)
{
// We do expect to get here because task-based continuations are scheduled even when the antecedent task throws.
try
{
cout << "received " << previousTask.get() << "from C2" << endl;
}
catch (const exception& e)
{
cout << "caught exception from previous task" << endl;
}
});
// wait for the continuations to finish.
try
{
cout << "waiting for tasks to finish..." << endl;
(c1 && c2).wait();
}
catch (const exception& e)
{
cout << "caught exception while waiting for all tasks to finish." << endl;
}Set up the URL/URI.
Create and sent the http request.
When the response header arrives, extract the data from the body and do the business logic to process the data.
Error/exception handling.
If you need the return value of the asynchronous tasks. Then use task::get or task::wait to wait for all asynchronous tasks to complete and handle all possible exception.
Otherwise, add a task based continuation to handle all possible exceptions.
For more details, you may visit this confluence page.