[知识点]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 方法可以捕获异步任务中抛出的异常。
相关推荐
Coding小公仔1 小时前
C++ bitset 模板类
开发语言·c++
凌肖战1 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
菜鸟看点1 小时前
自定义Cereal XML输出容器节点
c++·qt
小赖同学啊1 小时前
物联网数据安全区块链服务
开发语言·python·区块链
shimly1234562 小时前
bash 脚本比较 100 个程序运行时间,精确到毫秒,脚本
开发语言·chrome·bash
weixin_478689762 小时前
十大排序算法汇总
java·算法·排序算法
IT_10242 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
luofeiju2 小时前
使用LU分解求解线性方程组
线性代数·算法
new_zhou3 小时前
Windows qt打包编译好的程序
开发语言·windows·qt·打包程序