我在我的应用的std::async
部分使用C++
来执行heavy transaction
,如果网络出现问题,可能需要花费很长时间。{1}}所以我使用std::async
结合std::future
等待超时设置,这可以避免在交易耗时很长时挂断。每次用户点击UI
上的某个按钮时都会调用它。
我的C++
代码在4个不同的平台上使用,即iOS, Android, OSX & Windows
。以下是我使用std::async
来执行此操作的方法。
//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
}
以上std::async & std::future_status
技术适用于所有平台。仅在iOS
上,我遇到问题,future
在用户点击按钮的每个备用时间超时。
我使用std::async & std::future_status
的方式是否应该纠正?什么可能是错的?我试图搜索这个很多。我没有得到std::async
尚未为所有平台做好准备的信息。我是否遇到了std::async
iOS
上出现的问题?
我是基于async+futures
C++
编程的新手。如果我在这里做了一个明显的错误,请告诉我