asio 协程中 yield

来源:互联网 发布:linux如何打开pdf文件 编辑:程序博客网 时间:2024/06/16 04:44

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.


原创粉丝点击