I am using an std::async
in the C++
part of my app to do a heavy transaction
which has chances of taking a very long time in case of something wrong with the network. So I use an std::async
combined with std::future
waiting for a time out set which saves me from getting hung up when the transaction takes very very long. It gets called every time the user clicks a certain button on UI
.
This C++
code of mine gets used on 4 different platforms, namely iOS, Android, OSX & Windows
. Following is way I use std::async
to do this heavy operation.
//Do the operation in async
std::future<size_t> my_future_result(std::async(std::launch::async, [this]() {
size_t result = someHeavyFunctionCall();
return result;
}));
//try to get the status of the operation after a time_out
std::future_status my_future_status = my_future_result.wait_for(std::chrono::milliseconds(some_time_out));
if (my_future_status == std::future_status::timeout) {
std::cout << "it times out every alternate time on ios only" << std::endl;
}
else if (my_future_status == std::future_status::ready) {
if (my_future_result.get() > 0)
//we are all fine
}
Above std::async & std::future_status
technique works fine on all platforms. Its only on iOS
, I get a problem where the future
times out every alternate time the user clicks the button.
Is there something I should correct upon the way I am using std::async & std::future_status
? What might be wrong ? I have tried to search this a lot. I don't get anything other that the information that std::async
is not ready for all platforms. Am I hitting a problem that std::async
has on iOS
?
I am new to this kind of async+futures
based C++
programming. Do, let me know if I am doing an obvious mistake here