【C/C++】标准库之 future

本文探讨了C/C++标准库中的future概念,解释了future如何提供异步访问值的能力,特别是在多线程环境中。重点介绍了future的get函数,它会等待异步操作完成并返回结果。此外,还介绍了promise,作为future的提供者,如何创建并设置future的值。通过一个示例展示了promise如何与新线程协同工作,完成异步任务。
摘要由CSDN通过智能技术生成

Backto C/C++ Index


future 可以理解为金融中的 期货. 我有一个 future 类型的变量, 交给一个异步的线程去处理. 我到期来提货交割就可以了.这个能提供 future 服务的就叫provider.

Header with facilities that allow asynchronous access to values set by specific providers, possibly in a different thread.

Each of these providers (which are either promise or packaged_task objects, or calls to async) share access to a shared state with a future object: the point where the provider makes the shared state ready is synchronized with the point the future object accesses the shared state.

future 初探

// future example
#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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值