【发布时间】:2015-01-15 22:43:32
【问题描述】:
我正在尝试为 boost::asio 编写自定义异步函数,如 here 所述。
但是我得到 boost::coroutines::detail::forced_unwind 异常与 result.get
#include <boost/chrono.hpp>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/steady_timer.hpp>
#include <iostream>
namespace asio = ::boost::asio;
template <typename Timer, typename Token>
auto my_timer (Timer& timer, Token&& token)
{
typename asio::handler_type<Token,
void (::boost::system::error_code const)>::type
handler (std::forward<Token> (token));
asio::async_result<decltype (handler)> result (handler);
timer.async_wait (handler);
return result.get (); // Got forced_unwind exception here.
}
int main ()
{
asio::io_service io;
asio::steady_timer timer (io, ::boost::chrono::seconds (1));
asio::spawn (io, [&] (asio::yield_context yield)
{
try {
std::cout << "my_timer enter\n";
my_timer (timer, yield);
std::cout << "my_timer returns\n";
}
catch (const boost::coroutines::detail::forced_unwind& e)
{
std::cout << "boost::coroutines::detail::forced_unwind\n";
}
}
);
io.run ();
}
Coliru 上的代码相同
更新:
行为存在于:
Darwin 14.0.0 (MacOS 10.10)
clang version 3.6.0 (trunk 216817) and gcc version 4.9.1 (MacPorts gcc49 4.9.1_1)
boost 1.57
和
Red Hat 6.5
gcc version 4.7.2 20121015 (Red Hat 4.7.2-5) (GCC)
boost 1.57 and 1.56
(the example code was trivially modified because gcc 4.7 does not support c++14 mode)
【问题讨论】:
标签: c++ boost boost-asio coroutine