[知识点]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 方法可以捕获异步任务中抛出的异常。
相关推荐
Swift社区4 小时前
LeetCode 394. 字符串解码(Decode String)
算法·leetcode·职场和发展
另寻沧海4 小时前
测试中的 AAA 模式与 Given–When–Then 模式详解
c++·单元测试·测试覆盖率
tt5555555555555 小时前
LeetCode进阶算法题解详解
算法·leetcode·职场和发展
让我们一起加油好吗5 小时前
【基础算法】DFS中的剪枝与优化
算法·深度优先·剪枝
执尺量北斗6 小时前
[特殊字符] 基于 Qt + OpenGL 实现的入门级打砖块游戏
开发语言·qt·游戏
夏子曦6 小时前
C#内存管理深度解析:从栈堆原理到高性能编程实践
开发语言·c#
Q741_1476 小时前
C++ 模拟题 力扣495. 提莫攻击 题解 每日一题
c++·算法·leetcode·模拟
Felven6 小时前
A. Be Positive
算法
小O的算法实验室6 小时前
2026年COR SCI2区,自适应K-means和强化学习RL算法+有效疫苗分配问题,深度解析+性能实测,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
青岛少儿编程-王老师7 小时前
CCF编程能力等级认证GESP—C++7级—20250927
数据结构·c++·算法