金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  423 随笔 :: 0 文章 :: 454 评论 :: 0 Trackbacks

asio 协程中 yield


(金庆的专栏 2017.12)


https://stackoverflow.com/questions/26127458/yielding-in-boost-asio-stackful-coroutine


Asio spawn() 可以产生一个协程,协程中可以调用 async_read(..., yield), async_write(..., yield), 但是不知道如何主动释放控制权(yield)?


asio::spawn(strand_, [this, self](asio::yield_context yield)
{
    while (!computationFinished)
    {
        computeSomeMore();
        yield; // WHAT SHOULD THIS LINE BE?
    }
}


答案是:

iosvc.post(yield);


其他还可以是

iosvc.poll_one();

iosvc.poll();


应该是 post(yield) 最合适。


... polling the io_service avoids the context switch overhead, but unhandled exceptions from handlers will unwind and destroy the coroutine.


posted on 2017-12-07 14:51 金庆 阅读(1322) 评论(0)  编辑 收藏 引用 所属分类: 1. C/C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理