[知识点]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 方法可以捕获异步任务中抛出的异常。
相关推荐
科大饭桶9 分钟前
C++入门自学Day14-- Stack和Queue的自实现(适配器)
c语言·开发语言·数据结构·c++·容器
tt55555555555529 分钟前
字符串与算法题详解:最长回文子串、IP 地址转换、字符串排序、蛇形矩阵与字符串加密
c++·算法·矩阵
扛麻袋的少年1 小时前
7.Kotlin的日期类
开发语言·微信·kotlin
元亓亓亓1 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
钢铁男儿2 小时前
Python 正则表达式实战:解析系统登录与进程信息
开发语言·python·正则表达式
rainFFrain2 小时前
Boost搜索引擎项目(详细思路版)
网络·c++·http·搜索引擎
不会学习?2 小时前
算法03 归并分治
算法
野生技术架构师2 小时前
2025年中高级后端开发Java岗八股文最新开源
java·开发语言
long_run2 小时前
C++之模板函数
c++
NuyoahC2 小时前
笔试——Day43
c++·算法·笔试