清理一个超时的未来
Clean-up a timed-out future
我需要运行一个超时的函数。如果它没有在给定的超时内返回,我需要丢弃它并回退到另一个方法。
下面是一个(极大地(简化的示例代码来强调这个问题。(事实上,这是一个始终运行的、高可用的应用程序。在那里,我首先从缓存中读取,只有当缓存中有过时的数据时,我才会尝试从数据库中读取。但是,如果数据库查询耗时很长,我需要继续处理过时的数据。(
我的问题是,在未来阅读超时的情况下,我是否必须单独处理未来的清理工作(即保留一份副本,并不时检查是否准备好(?或者我可以简单地忽略它(即保持代码原样(。
/* DB query can be time-consuming, but the result is fresh */
string readFromDatabase(){
// ...
// auto dbValue = db.query("select name from users where id=" + _id);
// ...
return dbValue;
}
/* Cache query is instant, but the result could be stale */
string readFromLocalCache(){
// ...
// auto cachedVal = _cache[_id];
// ...
return cachedVal;
}
int getValue(){
// Idea:
// - Try reading from the database.
// - If the db query didn't return within 1 second, fallback to the other method.
using namespace std::chrono_literals;
auto fut = std::async(std::launch::async, [&](){ return readFromDatabase(); });
switch (fut.wait_for(1s)){
case std::future_status::ready: // query returned within allotted time
{
auto freshVal = fut.get();
// update cache
return freshVal;
}
case std::future_status::timeout: // timed out, fallback ------ (*)
{
break;
}
case std::future_status::deferred: // should not be reached
{
break;
}
}
return readFromLocalCache();
// quetion? what happens to `fut`?
}
我的问题是,在未来读取超时的情况下,我是否必须单独处理未来的清理(即保留一份副本,并不时检查它是否准备好(?或者我可以简单地忽略它(即保持代码原样(。
从我个人的角度来看,这取决于你想要什么。在当前(最小(实现下,getValue
函数将被未来的析构函数阻塞(请参阅cppreference页面和一些SO问题(。
如果你不想要阻塞行为,有一些解决方案,正如这个问题中提出的,比如:
- 将
future
移动到某个外部范围 - 使用分离的执行器和一些方便的代码/数据结构来处理返回状态
- 看看是否可以用一些超时支持I/O操作(如选择/轮询(替换
future
等。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '