現: 2017-10-25 (水) 09:59:24 takatsuka[3] [4] | |||
---|---|---|---|
Line 1: | Line 1: | ||
+ | スタックフルコルーチンはとても便利なんですが、コルーチン内で実行するには都合が悪いコードもあるかなと思います。 | ||
+ | そんなとき、表コンテキスト(って言うのだろうか。通常スタックの状態のこと。)で処理をさせて結果を得るっていうことを、同期処理っぽく書くやり方を、忘れる自信があるので備忘録です。 | ||
+ | #prettify{{ | ||
+ | using namespace boost; | ||
+ | |||
+ | asio::io_service ioService; | ||
+ | |||
+ | asio::spawn( ioService, | ||
+ | [&](asio::yield_context yield) | ||
+ | { | ||
+ | |||
+ | system::error_code ec; | ||
+ | asio::handler_type<asio::yield_context,void(system::error_code,int)>::type handler( std::forward<asio::yield_context>(yield[ec]) ); | ||
+ | asio::async_result<decltype(handler)> result(handler); | ||
+ | ioService.post( [handler]()mutable | ||
+ | { | ||
+ | int no = 123; // ここは表コンテキストで処理される | ||
+ | handler( system::error_code(), no ); | ||
+ | }); | ||
+ | int r = result.get(); // handler(・・・) が処理されるまで待つ | ||
+ | |||
+ | }); | ||
+ | }} |
(This host) = https://thinkridge.com