[知识点]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 方法可以捕获异步任务中抛出的异常。
相关推荐
Humbunklung7 分钟前
Rust Floem UI 框架使用简介
开发语言·ui·rust
csdnzzt11 分钟前
从内存角度透视现代C++关键特性
c++
jie1889457586639 分钟前
C++ 中的 const 知识点详解,c++和c语言区别
java·c语言·c++
网安INF44 分钟前
RSA加密算法:非对称密码学的基石
java·开发语言·密码学
明月*清风44 分钟前
c++ —— 内存管理
开发语言·c++
蔡蓝1 小时前
设计模式-观察着模式
java·开发语言·设计模式
WindSearcher1 小时前
大模型微调相关知识
后端·算法
取酒鱼食--【余九】1 小时前
rl_sar实现sim2real的整体思路
人工智能·笔记·算法·rl_sar
西北大程序猿2 小时前
单例模式与锁(死锁)
linux·开发语言·c++·单例模式
你不是我我2 小时前
【Java开发日记】说一说 SpringBoot 中 CommandLineRunner
java·开发语言·spring boot