C++异步调用Future机制漫谈
来源:互联网 发布:印度红色走廊 知乎 编辑:程序博客网 时间:2024/05/17 02:27
C++异步调用Future机制漫谈
一. 概念
单机:同步调用(us级)。
分布式:ms级,同步调用无法利用CPU优势,故异步调用。
典型场景:RPC、IO。
Future满足三点:
-> 异步调用基础设施;
-> 顾名思义,未来要完成的某件事;
-> 不block当前线程,空闲时再处理。
二. 实现要点
可以参考std::Future的实现机制。
std::future是一个非常有用也很有意思的东西,简单说std::future提供了一种访问异步操作结果的机制。从字面意思来理解,它表示未来,我觉得这个名字非常贴切,因为一个异步操作我们是不可能马上就获取操作结果的,只能在未来某个时候获取,但是我们可以以同步等待的方式来获取结果,可以通过查询future的状态(future_status)来获取异步操作的结果。future_status有三种状态:
- deferred:异步操作还没开始
- ready:异步操作已经完成
- timeout:异步操作超时
//查询future的状态std::future_status status; do { status = future.wait_for(std::chrono::seconds(1)); if (status == std::future_status::deferred) { std::cout << "deferred\n"; } else if (status == std::future_status::timeout) { std::cout << "timeout\n"; } else if (status == std::future_status::ready) { std::cout << "ready!\n"; } } while (status != std::future_status::ready);</span>
0 0
- C++异步调用Future机制漫谈
- Future 模式(异步调用)
- Future 模式(异步调用)
- Future模式(异步调用)
- Future结合RxJava 实现异步调用
- 从Java future 到 Guava ListenableFuture实现异步调用
- C++异步调用利器future/promise实现原理
- struts2实现异步调用机制
- Future异步计算
- Future异步计算
- Future异步事件驱动
- C函数调用机制
- C函数调用机制
- C函数调用机制
- c#_delegate_异步调用_BeginInvoke
- C#Task异步调用方法
- 调用机制:回调函数、同步调用、异步调用
- Struts2之异步调用机制详解
- 根据进程名称查找进程
- Android ListView等列表设置空布局
- 54. mysqli 扩展库
- Anorm, simple SQL data access
- Mysql函数:Last_insert_id()语法讲解
- C++异步调用Future机制漫谈
- ssh安全配置
- 搜索框制作(笔记)
- C++API设计
- Using "RDP Encoder Mirror Driver" to Capture Screen
- 根据进程名或者ID称关闭进程
- 第一篇博客
- linux常用命令locate,which,whereis,grep
- POJ 1270Following Orders(拓扑+DFS)