C++中,std::async 一个用于异步编程的工具

在C++中,std::async 是一个用于异步编程的工具,它允许你在一个单独的线程中执行任务,并返回一个 std::future 对象,通过这个对象可以获取任务的结果或者检查任务的状态。

基本用法1

lambda 表达式

复制代码
#include <iostream>
#include <future>
#include <chrono>

int main() {
    // 使用 std::async 创建一个异步任务
    auto future_result = std::async(std::launch::async, []{
        std::cout << "Task is running in a separate thread." << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(2));
        return 42;  // 返回值
    });

    // 主线程继续执行其他操作
    std::cout << "Main thread continues to execute." << std::endl;

    // 等待异步任务完成并获取结果
    int result = future_result.get();
    std::cout << "Result from async task: " << result << std::endl;

    return 0;
}

解释

  • std::launch::async: 强制任务在独立的线程中运行。
  • std::launch::deferred: 延迟执行任务,直到调用 future::get()future::wait()
  • std::launch::async | std::launch::deferred: 允许实现选择如何执行任务(默认行为)。

基本用法2

基本函数及传入函数值

复制代码
#include <iostream>
#include <future>
#include <chrono>

// 定义一个简单的异步函数
int calculate(int x) {
    std::this_thread::sleep_for(std::chrono::seconds(2));  // 模拟耗时操作
    return x * x;  // 返回平方值
}

int main() {
    // 使用 std::async 启动异步任务
    auto future_result = std::async(std::launch::async, calculate, 5);

    // 主线程继续执行其他操作
    std::cout << "Main thread continues to execute." << std::endl;

    // 等待异步任务完成并获取结果
    try {
        int result = future_result.get();  // 获取异步任务的返回值
        std::cout << "Result from async task: " << result << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Exception caught: " << e.what() << std::endl;
    }

    return 0;
}

输出示例

复制代码
Main thread continues to execute.
Task is running in a separate thread.
Result from async task: 25

解释

  1. 异步函数定义 : calculate 是一个普通的函数,接受一个整数参数并返回其平方值。
  2. 启动异步任务 : 使用 std::async 启动异步任务,传入 calculate 函数及其参数 5
  3. 主线程继续执行: 在等待异步任务完成的同时,主线程可以继续执行其他操作。
  4. 获取结果 : 使用 future_result.get() 等待异步任务完成并获取返回值。

特点

  1. 线程管理 : std::async 自动管理线程生命周期,简化了多线程编程。
  2. 结果获取 : 可以通过 std::future 获取异步任务的结果。
  3. 异常处理 : 如果异步任务抛出异常,可以通过 std::futureget() 方法捕获。

注意事项

  • std::async 并不保证一定创建新线程,具体行为取决于实现和参数设置。

  • 使用 std::futureget() 方法可能会阻塞当前线程,直到异步任务完成。

  • 异常处理 : 如果异步任务抛出异常,future::get() 会重新抛出该异常,因此建议使用 try-catch 块来捕获异常。

  • 线程安全: 如果多个线程访问共享资源,确保采取适当的同步措施。

相关推荐
QX_hao11 分钟前
【Go】--接口(interface)
开发语言·后端·golang
西西学代码33 分钟前
Flutter---个人信息(1)---实现简单的UI
开发语言·javascript·flutter
superman超哥35 分钟前
仓颉语言中正则表达式引擎的深度剖析与实践
开发语言·后端·仓颉
在坚持一下我可没意见1 小时前
Java 网络编程:TCP 与 UDP 的「通信江湖」(基于UDP回显服务器)
java·服务器·开发语言·tcp/ip·udp·java-ee
西红柿维生素1 小时前
23种设计模式-框架中的使用
java·开发语言·设计模式
LNN20221 小时前
Qt creator +Valgrind检测内存泄漏(linux)
linux·开发语言·qt
修炼前端秘籍的小帅2 小时前
精读《JavaScript 高级程序设计 第4版》第6章 集合引用类型(三)Map、WeakMap、Set、WeakSet
开发语言·javascript·ecmascript
@LetsTGBot搜索引擎机器人2 小时前
打造属于你的 Telegram 中文版:汉化方案 + @letstgbot 搜索引擎整合教程
开发语言·python·搜索引擎·机器人·.net
CS创新实验室2 小时前
典型算法题解:长度最小的子数组
数据结构·c++·算法·考研408
我有一些感想……2 小时前
浅谈 BSGS(Baby-Step Giant-Step 大步小步)算法
c++·算法·数论·离散对数·bsgs