[知识点]c++ future库 async方法

C++ 标准库中的 futureasync 提供了一种便捷的方式来实现并发编程。std::async 是一个非常强大的工具,它可以用于启动异步任务,并返回一个 std::future 对象,该对象可以用来等待任务的结果。

std::async 的基本用法

std::async 用于启动一个异步任务。它的基本语法如下:

cpp 复制代码
#include <iostream>
#include <future>
#include <thread>

// 一个简单的函数,模拟一些工作
int work(int x) {
    std::this_thread::sleep_for(std::chrono::seconds(2)); // 模拟延迟
    return x * x;
}

int main() {
    // 使用 std::async 启动异步任务,并获取 std::future 对象
    std::future<int> result = std::async(std::launch::async, work, 10);

    // 在此处可以执行其他工作
    std::cout << "Doing other work..." << std::endl;

    // 获取异步任务的结果(会阻塞直到结果准备好)
    int value = result.get();

    std::cout << "Result: " << value << std::endl;

    return 0;
}

std::async 启动模式

std::async 提供了两种启动模式:

  1. std::launch::async:任务在独立线程中异步执行。
  2. std::launch::deferred :任务延迟执行,直到 future 对象的 getwait 方法被调用。
示例:不同的启动模式
cpp 复制代码
#include <iostream>
#include <future>
#include <thread>

int work(int x) {
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return x * x;
}

int main() {
    // 异步启动
    std::future<int> async_result = std::async(std::launch::async, work, 10);

    // 延迟启动
    std::future<int> deferred_result = std::async(std::launch::deferred, work, 20);

    // 在此处可以执行其他工作
    std::cout << "Doing other work..." << std::endl;

    // 获取异步任务的结果(会阻塞直到结果准备好)
    int async_value = async_result.get();
    std::cout << "Async result: " << async_value << std::endl;

    // 获取延迟任务的结果(会在此时执行任务)
    int deferred_value = deferred_result.get();
    std::cout << "Deferred result: " << deferred_value << std::endl;

    return 0;
}

使用 std::future 获取结果

std::future 提供了多种方法来获取任务结果:

  • get:阻塞当前线程,直到任务完成并返回结果。
  • wait:阻塞当前线程,直到任务完成。
  • wait_for:阻塞当前线程一段时间,等待任务完成。
  • wait_until:阻塞当前线程直到指定时间,等待任务完成。
示例:使用 std::future 的不同方法
cpp 复制代码
#include <iostream>
#include <future>
#include <thread>

int work(int x) {
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return x * x;
}

int main() {
    std::future<int> result = std::async(std::launch::async, work, 10);

    // 等待一段时间
    if (result.wait_for(std::chrono::seconds(1)) == std::future_status::timeout) {
        std::cout << "Task is still running..." << std::endl;
    }

    // 再次等待,直到任务完成
    result.wait();
    int value = result.get();
    std::cout << "Result: " << value << std::endl;

    return 0;
}

捕获异常

如果异步任务在执行过程中抛出异常,std::futureget 方法会重新抛出该异常。可以通过捕获异常来处理任务中的错误。

示例:捕获异常
cpp 复制代码
#include <iostream>
#include <future>
#include <stdexcept>
#include <thread>

int work(int x) {
    if (x < 0) {
        throw std::invalid_argument("x must be non-negative");
    }
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return x * x;
}

int main() {
    // 启动一个会抛出异常的任务
    std::future<int> result = std::async(std::launch::async, work, -10);

    try {
        int value = result.get(); // 这里会重新抛出异常
        std::cout << "Result: " << value << std::endl;
    } catch (const std::exception& e) {
        std::cout << "Exception: " << e.what() << std::endl;
    }

    return 0;
}

总结

  • std::async :用于启动异步任务,可以指定启动模式(asyncdeferred)。
  • std::future :用于获取异步任务的结果,提供多种方法(get, wait, wait_for, wait_until)。
  • 异常处理 :通过 std::futureget 方法可以捕获异步任务中抛出的异常。
相关推荐
电饭叔12 分钟前
《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学
开发语言·python
Eternal-Student13 分钟前
everyday_question dq20240731
开发语言·arm开发·php
卑微求AC28 分钟前
(C语言贪吃蛇)11.贪吃蛇方向移动和刷新界面一起实现面临的问题
c语言·开发语言
程序猿小D41 分钟前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa
wangyue443 分钟前
c# 线性回归和多项式拟合
算法
Yvemil71 小时前
RabbitMQ 入门到精通指南
开发语言·后端·ruby
潘多编程1 小时前
Java中的状态机实现:使用Spring State Machine管理复杂状态流转
java·开发语言·spring
&梧桐树夏1 小时前
【算法系列-链表】删除链表的倒数第N个结点
数据结构·算法·链表
QuantumStack1 小时前
【C++ 真题】B2037 奇偶数判断
数据结构·c++·算法
今天好像不上班1 小时前
软件验证与确认实验二-单元测试
测试工具·算法