C++中Boost Coroutine使用协程详解

发布时间:

通过Boost.Coroutine,可以在C++中使用协程。协程是其他编程语言的一个特性,通常使用关键字yield来表示协程。在这些编程语言中,yield可以像return一样使用!

C++中Boost Coroutine使用协程详解

一、说明语言扩展

以下库扩展了编程语言 C++。

  • Boost.Coroutine 使得在 C++ 中使用协程成为可能--其他编程语言通常通过关键字 yield 支持。
  • Boost.Foreach 提供了一个基于范围的 for 循环,它是在 C++11 中添加到语言中的。
  • Boost.Parameter 允许您以名称/值对的形式并以任何顺序传递参数--例如,这在 Python 中是允许的。
  • Boost.Conversion 提供了两个转换运算符来替换 dynamic_cast 并允许您区分向下转换和交叉转换。

二、库Boost.Coroutine

通过 Boost.Coroutine,可以在 C++ 中使用协程。协程是其他编程语言的一个特性,通常使用关键字 yield 来表示协程。在这些编程语言中,yield 可以像 return 一样使用。但是,当使用 yield 时,该函数会记住该位置,如果再次调用该函数,将从该位置继续执行。

C++ 没有定义关键字 yield。但是,使用 Boost.Coroutine 可以从函数返回并稍后从同一位置继续。 Boost.Asio 库也使用 Boost.Coroutine 并受益于协程。

三、示例和代码

Boost.Coroutine 有两个版本。本章介绍第二个版本,即当前版本。这个版本从 Boost 1.55.0 开始可用,并取代了第一个版本。

示例 51.1。使用协程

#include <boost/coroutine/all.hpp>
#include <iostream>
using namespace boost::coroutines;
void cooperative(coroutine<void>::push_type &sink)
{
  std::cout << "Hello";
  sink();
  std::cout << "world";
}
int main()
{
  coroutine<void>::pull_type source{cooperative};
  std::cout << ", ";
  source();
  std::cout << "!\n";
}

Example51.1

示例 51.1 定义了一个函数 cooperative(),它作为协程从 main() 调用。 cooperative() 提前返回 main() 并被第二次调用。在第二次调用时,它会从中断处继续。

要将 cooperative() 用作协程,使用类型 pull_type 和 push_type。这些类型由 boost::coroutines::coroutine 提供,这是一个在示例 51.1 中用 void 实例化的模板。

要使用协程,您需要 pull_type 和 push_type。其中一种类型将用于创建一个对象,该对象将使用您想用作协程的函数进行初始化。另一种类型将是协程函数的第一个参数。

示例 51.1 在 main() 中创建了一个名为 source 的 pull_type 类型的对象。 cooperative() 传递给构造函数。 push_type 用作 cooperative() 签名中的唯一参数。

创建源时,传递给构造函数的函数 cooperative() 会立即作为协程调用。发生这种情况是因为源基于 pull_type。如果源基于 push_type,则构造函数不会将 cooperative() 作为协程调用。

cooperative() 将 Hello 写入标准输出。之后,函数像访问函数一样访问 sink 。这是可能的,因为 push_type 重载了 operator()。 main() 中的 source 表示协程 cooperative(),cooperative() 中的 sink 表示函数 main()。调用 sink 使 cooperative() 返回,而 main() 从调用 cooperative() 的地方继续,并将逗号写入标准输出。

然后,main() 调用 source 就好像它是一个函数一样。同样,这是可能的,因为重载了 operator()。这一次,cooperative() 从中断点继续并将世界写入标准输出。因为 cooperative() 中没有其他代码,协程结束。它返回到 main(),它将一个感叹号写入标准输出。

结果是示例 51.1 显示 Hello, world!

您可以将协程视为协作线程。在某种程度上,函数 main() 和 cooperative() 同时运行。代码在 main() 和 cooperative() 中轮流执行。每个函数内的指令按顺序执行。多亏了协程,一个函数不需要在另一个函数执行之前返回。

示例 51.2。从协程返回一个值

#include <boost/coroutine/all.hpp>
#include <functional>
#include <iostream>
using boost::coroutines::coroutine;
void cooperative(coroutine<int>::push_type &sink, int i)
{
  int j = i;
  sink(++j);
  sink(++j);
  std::cout << "end\n";
}
int main()
{
  using std::placeholders::_1;
  coroutine<int>::pull_type source{std::bind(cooperative, _1, 0)};
  std::cout << source.get() << '\n';
  source();
  std::cout << source.get() << '\n';
  source();
}

Example51.2

示例 51.2 与前面的示例类似。这次模板 boost::coroutines::coroutine 是用 int 实例化的。这使得从协程返回一个 int 给调用者成为可能。

传递 int 值的方向取决于使用 pull_type 和 push_type 的位置。该示例使用 pull_type 在 main() 中实例化一个对象。 cooperative() 可以访问 push_type 类型的对象。 push_type 发送一个值,pull_type 接收一个值;因此,设置了数据传输的方向。

cooperative() 调用 sink,参数类型为 int。此参数是必需的,因为协程是使用数据类型 int 实例化的。通过使用由 pull_type 提供的成员函数 get() 从 main() 中的 source 接收传递给 sink 的值。

示例 51.2 还说明了如何将具有多个参数的函数用作协程。 cooperative() 有一个额外的 int 类型参数,不能直接传递给 pull_type 的构造函数。该示例使用 std::bind() 将函数与 pull_type 链接起来。

该示例将 1 和 2 后跟 end 写入标准输出。

示例 51.3。将两个值传递给协程

#include <boost/coroutine/all.hpp>
#include <tuple>
#include <string>
#include <iostream>
using boost::coroutines::coroutine;
void cooperative(coroutine<std::tuple<int, std::string>>::pull_type &source)
{
  auto args = source.get();
  std::cout << std::get<0>(args) << " " << std::get<1>(args) << '\n';
  source();
  args = source.get();
  std::cout << std::get<0>(args) << " " << std::get<1>(args) << '\n';
}
int main()
{
  coroutine<std::tuple<int, std::string>>::push_type sink{cooperative};
  sink(std::make_tuple(0, "aaa"));
  sink(std::make_tuple(1, "bbb"));
  std::cout << "end\n";
}

Example51.3

示例 51.3 在 main() 中使用 push_type,在 cooperative() 中使用 pull_type,这意味着数据从调用方传输到协程。

此示例说明如何传递多个值。 Boost.Coroutine 不支持传递多个值,因此必须使用元组。您需要将多个值打包到元组或其他结构中。

示例 51.3 显示 0 aaa、1 bbb 和 end。

示例 51.4。协程和异常

#include <boost/coroutine/all.hpp>
#include <stdexcept>
#include <iostream>
using boost::coroutines::coroutine;
void cooperative(coroutine<void>::push_type &sink)
{
  sink();
  throw std::runtime_error("error");
}
int main()
{
  coroutine<void>::pull_type source{cooperative};
  try
  {
source();
  }
  catch (const std::runtime_error &e)
  {
std::cerr << e.what() << '\n';
  }
}

协程在抛出异常时立即返回。异常被传输到协程的调用者,在那里它可以被捕获。因此,异常与常规函数调用没有什么不同。

示例 51.4 显示了这是如何工作的。此示例会将字符串错误写入标准输出。