# 协程

## 前言

既然是“**现代**” C++ 并发编程教程，怎么能不聊协程呢？

C++20 引入了协程语法，新增了三个用作协程的关键字：`co_await`、`co_yield`、`co_return`。但并未给出标准**协程库**，协程库在 C++23 被引入。

希望您拥有 `gcc14`、`clang19`，`Visual Studio 2022 17.11`。

我们假设您对 C++20 的协程一无所知、假设您对协程这个概念一无所知、假设您不了解其它语言的协程实现（如 Python、java）。

***

绝大多数人对协程基本可以说是一无所知，但是应该都听过这个名字，大概是因为这些编程语言都在新版本中引入它作为核心语言特性。

这带来了许多的热度，不过这并不完全算是好事，许多的营销号一样的讲述，基本全部都是错误的。

据我所知，在我在 B站发布正经 C++20 协程的教学视频之前，几乎所有打着 C++ 旗号说什么协程的，都是胡言乱语。不过也有一些不错的，如：[**等疾风**](https://space.bilibili.com/35186937)、[**happyyang的百草园**](https://space.bilibili.com/312883756)，都出过至少算作正经的 C++20 协程的教学视频。

* **C++20 的协程是复杂的**。

不管是使用上还是概念上，引入了许多新颖的做法。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mq-bai.gitbook.io/modern-cpp-concurrent-programming/md/06-xie-cheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
