C++ 标准库 <future>

C++ 标准库 <future>
C++11 引入了 <future> 头文件,它提供了一种异步编程的机制,允许程序在等待某个操作完成时继续执行其他任务。<future> 库是 C++ 标准库中并发编程的一部分,它允许程序员以一种更简洁和安全的方式处理异步操作。

<future> 库中定义了几个关键的类型:

std::future:表示异步操作的结果,可以查询操作的状态,获取结果或等待操作完成。
std::promise:用于与 std::future 配对,用于设置异步操作的结果。
std::packaged_task:封装一个函数或可调用对象,使其可以作为异步任务执行。
std::promise
std::promise 用于设置异步操作的结果。它与 std::future 配对使用。

实例
#include <iostream>
#include <future>

int main() {
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();

    // 在另一个线程中设置结果
    std::thread t([prom]() {
        prom.set_value(10);
    });

    // 等待结果
    std::cout << "Future value: " << fut.get() << std::endl;

    t.join();
    return 0;
}
输出结果:

Future value: 10
std::packaged_task
std::packaged_task 封装一个函数或可调用对象,使其可以作为异步任务执行。

实例
#include <iostream>
#include <future>
#include <cmath>

int compute_square_root(double x) {
    return std::sqrt(x);
}

int main() {
    std::packaged_task<double(double)> task(compute_square_root);
    std::future<double> result = task.get_future();
    std::thread th(std::move(task), 9.0);

    std::cout << "Result: " << result.get() << std::endl;
    th.join();
    return 0;
}
输出结果:

Result: 3
std::async
std::async 是一个方便的函数,用于启动异步任务。它可以立即返回一个 std::future 对象。

实例
#include <iostream>
#include <future>

int main() {
    std::future<int> fut = std::async(std::launch::async, [](int x) {
        return x * x;
    }, 5);

    std::cout << "Result: " << fut.get() << std::endl;
    return 0;
}
输出结果:

Result: 25
异常处理
当异步操作抛出异常时,std::future 会捕获这个异常,并且可以通过调用 .get() 方法来重新抛出它。

实例
#include <iostream>
#include <future>

void throw_exception() {
    throw std::runtime_error("Exception thrown");
}

int main() {
    std::future<void> fut = std::async(throw_exception);

    try {
        fut.get();
    } catch (const std::exception& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    return 0;
}
输出结果:

Caught exception: Exception thrown
<future> 库为 C++ 程序员提供了一种简单而强大的异步编程方式。通过使用 std::promise、std::packaged_task 和 std::async,我们可以轻松地在 C++ 程序中实现并发和异步操作。同时,异常处理机制也确保了程序的健壮性。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值