c++ - 使用 Boost Coroutines 实现多任务调度器和执行器

标签 c++ python-3.x c++11 boost c++14

我正在寻找一种方法来使用 Boost Stackfull Coroutines 作为单核中的多任务执行器,而不是使用线程。我习惯于使用 asyncio module 开发 Python 代码我想使用 Boost Coroutines(如果可能的话是 Boost.Coroutine2)复制相同的行为。

我在 Google 上的搜索返回了 some old Boost documentation ,解释了我如何使用 Boost.Coroutine lib 来做到这一点。 出于某种我不知道的原因,the current Boost.Coroutine documentation不相等,不包含任何可能与旧版本相关的信息。

有没有办法让我像使用 Python 异步协程一样使用 Boost.Coroutines?

最佳答案

我喜欢在 Boost.Asio 中使用协程,因为后者甚至有 some integration与前者。

boost::asio::io_service io_service;

// ...

boost::asio::spawn(io_service, [&] (boost::asio::yield_context yield) {
    // here, the asynchronous operations causes the coroutine to yield
    std::size_t bytes = socker.async_read_some(buffer, yield);
    // do some other stuff
});

io_service.run();

Boost.Asio 有自己的 full example .

io_service 将作为执行器工作,spawn 提供了一种使用协同程序完成所有这些工作的清晰方法。

关于c++ - 使用 Boost Coroutines 实现多任务调度器和执行器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36921286/

相关文章:

c++ - QTableView选择单个单元格

c++ - 带有返回值的 if 语句代码样式

python - 通过 pip 将包安装到单独的目录

c++ - -std=c++0x 导致使用 boost 1.64 返回对临时的引用

c++ - Xcode 4.2 和 lambda 表达式 (OSX Lion)

C++:如何解决隐式声明编译错误?

c++ - 为函数创建别名

python - 比较单列的行元素。如果有 2 个连续的 L,则从 High 列中选择最低的,忽略其他。相反,如果 2 L

python - 根据 pandas DataFrame 中的一系列值生成索引元组

c++ - 如何独立于标准启用 GCC 的 GNU 扩展?