Assume we have the sequential code:
void process_login(std::string const& username,std::string const& password) {
try {
user_id const id=backend.authenticate_user(username,password);
user_data const info_to_display=backend.request_current_info(id);
update_display(info_to_display);
} catch(std::exception& e) {
display_error(e);
}
}
Which we want to make concurrent, since we don't want to block the UI Thread. So we first do it with a single std::async [2]
std::future<void> process_login(std::string const& username,std::string const& password) {
return std::async(std::launch::async,[=]() {
try {
user_id const id=backend.authenticate_user(username,password);
user_data const info_to_display=
backend.request_current_info(id);
update_display(info_to_display);
} catch(std::exception& e){
display_error(e);
}});
}
However according to the author: "using one std::async would still block the new thread, consuming resources while waiting for the tasks to complete."