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.