【现代C++】协程的使用

协程是C++20引入的一项重要特性,它允许函数的执行被暂停并在之后恢复,非常适合用于异步编程、数据生产者/消费者模式、状态机等场景。协程的引入旨在提供一种更简洁和直观的方式来处理异步操作和非阻塞编程。以下是C++协程的关键概念和使用示例:

1. 协程基础

在C++中,协程是通过特殊的函数实现的,这些函数可以使用co_awaitco_returnco_yield关键字来暂停和恢复执行。

cpp 复制代码
#include <coroutine>
#include <iostream>
#include <future>

std::future<void> simpleCoroutine() {
    std::cout << "Hello, ";
    co_await std::suspend_always{};
    std::cout << "World!" << std::endl;
    co_return;
}

void runSimpleCoroutine() {
    auto future = simpleCoroutine();
    future.get();  // 输出:Hello, World!
}

2. 使用co_yield生成值

协程可以使用co_yield生成一系列值,非常适合实现生成器模式。

cpp 复制代码
#include <coroutine>
#include <iostream>
#include <vector>

struct Generator {
    struct promise_type {
        int current_value;
        std::suspend_always yield_value(int value) {
            current_value = value;
            return {};
        }
        std::suspend_never initial_suspend() { return {}; }
        std::suspend_never final_suspend() noexcept { return {}; }
        Generator get_return_object() { return Generator{this}; }
        void return_void() {}
        void unhandled_exception() { std::exit(1); }
    };

    std::coroutine_handle<promise_type> coro;

    Generator(promise_type* p) : coro(std::coroutine_handle<promise_type>::from_promise(*p)) {}
    ~Generator() { if (coro) coro.destroy(); }

    int getValue() { return coro.promise().current_value; }
    bool next() { coro.resume(); return !coro.done(); }
};

Generator generateNumbers() {
    for (int i = 0; i < 5; ++i) {
        co_yield i;
    }
}

void runGenerateNumbers() {
    auto gen = generateNumbers();
    while (gen.next()) {
        std::cout << gen.getValue() << " ";
    }
    // 输出:0 1 2 3 4
}

3. 使用co_await等待异步操作

co_await表达式可以暂停当前协程的执行,直到等待的对象准备就绪。

cpp 复制代码
#include <coroutine>
#include <future>
#include <iostream>

std::future<int> asyncComputation() {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return std::async(std::launch::async, []() { return 42; });
}

std::future<void> awaitCoroutine() {
    int result = co_await asyncComputation();
    std::cout << "Result: " << result << std::endl;
    co_return;
}

void runAwaitCoroutine() {
    auto future = awaitCoroutine();
    future.get();  // 输出:Result: 42
}

4. 协程与异常处理

协程支持异常处理,你可以在协程内部使用try/catch块来处理异常。

cpp 复制代码
#include <coroutine>
#include <iostream>
#include <stdexcept>

std::future<void> exceptionCoroutine() {
    try {
        throw std::runtime_error("Example exception");
        co_return;
    } catch (const std::exception& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
}

void runExceptionCoroutine() {
    auto future = exceptionCoroutine();
    future.get();  // 输出:Caught exception: Example exception
}

通过这些示例可以看到C++协程如何用于不同的场景,它们提供了一种更直观和简洁的方式来处理异步操作、生成数据和编写复杂的控制流逻辑。

相关推荐
重启的码农1 小时前
llama.cpp 分布式推理介绍(2) 后端注册机制 (Backend Registration)
c++·人工智能·神经网络
重启的码农1 小时前
llama.cpp 分布式推理介绍(1) 远程计算设备 (RPC Device)
c++·人工智能·神经网络
hllqkbb1 小时前
从 SGD 到梯度累积:Epoch、Batch、Step 的关系全解析
开发语言·人工智能·opencv·计算机视觉·batch
风吹落叶32571 小时前
深入解析JVM内存管理与垃圾回收机制
java·开发语言·jvm
蜗牛沐雨2 小时前
C++ 类型转换
开发语言·c++
_poplar_2 小时前
08.5【C++ 初阶】实现一个相对完整的日期类--附带源码
c语言·开发语言·数据结构·c++·vscode·算法·vim
意疏2 小时前
探秘C语言:数据在内存中的存储机制详解
c语言·开发语言
源远流长jerry3 小时前
STM32之MCU和GPIO
linux·c++·stm32·单片机·嵌入式硬件
汤永红3 小时前
week2-[一维数组]最大元素
数据结构·c++·算法·信睡奥赛
Minecraft红客6 小时前
C++小游戏荒芜的城堡
c++·游戏·娱乐