我正在寻找一种方法来使用 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/