C++future promise
A future is an object that can retrieve a value from some provider object or function, properly synchronizing this access if in different threads.
provider比较抽象,可以是函数可以是类,是别人提供好的东西
A promise is an object that can store a value of type T to be retrieved by a future object (possibly in another thread), offering a synchronization point.
future用于访问异步操作结果
std::async可以异步执行函数,可能是放到另一个线程去执行,返回值是一个future类型
std::promise用于线程同步的工具,储存一些数据,供future获取,协程在运行得到结果后也会将结果写入promise供获取
以async为例
代码来源
#include <iostream>             // std::cout
#include <future>               // std::async, std::future
#include <chrono>               // std::chrono::milliseconds
// a non-optimized way of checking for prime numbers:
bool is_prime(int x)
{
    for (int i = 2; i < x; ++i)
        if (x % i == 0)
            return false;
    return true;
}
int main()
{
    // call function asynchronously:
    std::future < bool > fut = std::async(is_prime, 6666661);
    // do something while waiting for function to set future:
    std::cout << "checking, please wait";
    std::chrono::milliseconds span(100);
    while (fut.wait_for(span) == std::future_status::timeout)
        std::cout << '.';
    bool x = fut.get();         // retrieve return value
    std::cout << "\n6666661 " << (x ? "is" : "is not") << " prime.\n";
    return 0;
}
后面C++20里的协程我猜应该是基于此实现的,理解一下有好处
这里放一下官方promise的代码
#include <iostream>       // std::cout
#include <functional>     // std::ref
#include <thread>         // std::thread
#include <future>         // std::promise, std::future
void print_int (std::future<int>& fut) {
  int x = fut.get();//获取异步执行结果
  std::cout << "value: " << x << '\n';
}
int main ()
{
  std::promise<int> prom;                      // create promise
  std::future<int> fut = prom.get_future();    // engagement with future
  std::thread th1 (print_int, std::ref(fut));  // send future to new thread
  prom.set_value (10);                         // 模拟fulfill promise
                                               // (synchronizes with getting the future)
  th1.join();
  return 0;
}
												
											C++future promise的更多相关文章
- folly教程系列之:future/promise
		
attension:本文严禁转载. 一.前言 promise/future是一个非常重要的异步编程模型,它可以让我们摆脱传统的回调陷阱,从而使用更加优雅.清晰的方式进行异步编程.c++11中 ...
 - Future Promise 模式(netty源码9)
		
netty源码死磕9 Future Promise 模式详解 1. Future/Promise 模式 1.1. ChannelFuture的由来 由于Netty中的Handler 处理都是异步IO ...
 - clojure的delay future promise
		
<Clojure编程>第4章笔记. 总的感觉,Clojure毕竟是基于JVM的在人间的工程化语言.不是纯的无状态纯函数的在神间的lisp. 作为后端语言,不可避免要处理计算中和代码执行时序 ...
 - based on Greenlets (via Eventlet and Gevent)    fork  孙子worker  比较 gevent不是异步 协程原理  占位符 placeholder (Future, Promise, Deferred)   循环引擎  greenlet    没有显式调度的微线程,换言之 协程
		
gevent GitHub - gevent/gevent: Coroutine-based concurrency library for Python https://github.com/gev ...
 - 使用 Vert.X Future/Promise 编写异步代码
		
Future 和 Promise 是 Vert.X 4.0中的重要角色,贯穿了整个 Vert.X 框架.掌握 Future/Promise 的用法,是用好 Vert.X.编写高质量异步代码的基础.本文 ...
 - The promises and challenges of std::async task-based parallelism in C++11  C++11 std::async/future/promise
		
转载 http://eli.thegreenplace.net/2016/the-promises-and-challenges-of-stdasync-task-based-parallelism- ...
 - Future与Promise
		
https://code.csdn.NET/DOC_Scala/chinese_scala_offical_document/file/Futures-and-Promises-cn.md#ancho ...
 - 并发编程(三)Promise, Future 和 Callback
		
并发编程(三)Promise, Future 和 Callback 异步操作的有两个经典接口:Future 和 Promise,其中的 Future 表示一个可能还没有实际完成的异步任务的结果,针对这 ...
 - Netty 中的异步编程 Future 和 Promise
		
Netty 中大量 I/O 操作都是异步执行,本篇博文来聊聊 Netty 中的异步编程. Java Future 提供的异步模型 JDK 5 引入了 Future 模式.Future 接口是 Java ...
 - 带callback的future实现
		
jdk暂时不支持,所以只有自己实现带callback的future. 完成后callback的 final TaskPromise promise = new DefaultTaskPromise() ...
 
随机推荐
- ERR_UNSAFE_PORT浏览器安全问题无法访问的解决方案
			
ERR_UNSAFE_PORT浏览器安全问题导致无法访问的解决方案目录 ERR_UNSAFE_PORT浏览器安全问题导致无法访问的解决方案 一.问题现象 二.浏览器自身机制 三.解决方法 1.Goog ...
 - FPGA MIG调试bug(二)
			
目标器件:复旦微FPGA:JFM7K325T8FCBGA676(对标Xilinx Kintex-7系的XC7K325T) 工程背景:送入FPGA的外部时钟为差分时钟,时钟送入FPGA后,经过PLL输出 ...
 - 从零开始升级基于RuleBased的聊天机器人
			
这里记录从最基础的基于规则的聊天机器人,升级到基于逻辑的机器人,再升级到调用Google提供的API来让机器人能说.会听普通话. 最基本的完全基于规则式的问答:问什么就答什么,幼儿园水平. impor ...
 - 微信电脑版 v3.9.2.12 绿色便携版
			
修改历史: 2023.03.01:自改官方 3.9.2.12 最新正式版本2023.01.31:自改官方 3.9.0.28 最新正式版本2023.01.11:自改官方 3.9.0.21 最新正式版本- ...
 - tcl编程
			
目录 0. 基础语法 0.1 普通变量 0.2 list, 列表 0.3 array, 数组 0.4 循环 0.4.1 for 0.4.2 foreach 1. 从命令行获取参数(好像并不是很强大) ...
 - kolla
			
kolla项目是为了容器化openstack,目标是做到100个节点的开箱即用,所有的组件的HA都具备.kolla是一个革命性的项目,我们以前积累的安装部署经验,全部都报废.使用kolla可以快速部署 ...
 - 用例需注意的点-UI自动化
			
记几条--用例注意事项:用例从功能里面转化而来,并且不能脱离业务(针对某一个页面功能\某一个流程业务,写一条用例:即将界面操作间接转化为代码去操作!)1用例要尽量独立,相互不影响!(单独一条都可运行) ...
 - centos 换源
			
原文: https://blog.csdn.net/MateSnake/article/details/124088310 备份原来的源:sudo mv /etc/yum.repos.d/CentOS ...
 - Java中类似c语言的printf
			
System.out.printf("%4d",x); printf("%4d",x); 保留小数点后两位也可以用%.2f 相对来说很好记了 回车用\n
 - 【windows】 进程间通信  WM_COPYDATA消息
			
WM_COPYDATA消息可以实现window应用程序之间的数据传输,是同步传输方式. 需要用到结构体 COPYDATASTRUCT typedef struct tagCOPYDATASTRUCT ...