如果仍然有异步操作在等待basic_waitable_timer的破坏,该怎么办?行为记录在任何地方吗?
当IO对象(例如basic_waitable_timer
)被销毁时,其析构函数将调用destroy()
IO对象的服务(不要与混淆io_service
),并传递IO对象的实现。甲basic_waitable_timer
的服务是waitable_timer_service
与满足WaitableTimerService型的要求。WaitableTimerService的要求定义了destroy()
取消异步等待操作的后置条件,使异步等待操作尽快完成,并且已取消操作的处理程序将传递错误代码boost::asio::error::operation_aborted
。
service.destroy(impl);
→隐式取消异步等待操作,就像通过调用service.cancel(impl, e)
。
service.cancel(impl, e);
→使所有未完成的异步等待操作尽快完成。取消操作的处理程序应传递错误代码error::operation_aborted
。设置e
指示成功或失败。
请注意,已经排队等待调用的操作的处理程序将不会被取消,并且将具有一个error_code
反映操作成功的的处理程序。
这是演示此行为的完整示例:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
void demo_deferred_completion()
{
std::cout << "[demo deferred completion]" << std::endl;
boost::asio::io_service io_service;
auto wait_completed = false;
// Use scope to force lifetime.
{
// Create the timer and initiate an async_wait operation that
// is guaranteed to have expired.
boost::asio::steady_timer timer(io_service);
// Post a ready-to-run no-op completion handler into the io_service.
// Although the order is unspecified, the current implementation
// will use a predictable order.
io_service.post([]{});
// Initiate an async_wait operation that will immediately expire.
timer.expires_at(boost::asio::steady_timer::clock_type::now());
timer.async_wait(
[&](const boost::system::error_code& error)
{
std::cout << "error: " << error.message() << std::endl;
assert(error == boost::system::error_code()); // Success.
wait_completed = true;
});
// While this will only run one handler (the noop), it will
// execute operations (async_wait), and if they are succesful
// (time expired), the completion handler will be posted for
// deferred completion.
io_service.run_one();
assert(!wait_completed); // Verify the wait handler was not invoked.
} // Destroy the timer.
// Run the handle_wait completion handler.
io_service.run();
assert(wait_completed);
}
void demo_cancelled()
{
std::cout << "[demo cancelled]" << std::endl;
boost::asio::io_service io_service;
// Use scope to force lifetime.
{
boost::asio::steady_timer timer(io_service);
// Initiate an async_wait operation that will immediately expire.
timer.expires_at(boost::asio::steady_timer::clock_type::now());
timer.async_wait(
[](const boost::system::error_code& error)
{
std::cout << "error: " << error.message() << std::endl;
assert(error ==
make_error_code(boost::asio::error::operation_aborted));
});
} // Destroy the timer.
// Run the handle_wait completion handler.
io_service.run();
}
int main()
{
demo_deferred_completion();
demo_cancelled();
}
输出:
[demo deferred completion]
error: Success
[demo cancelled]
error: Operation canceled
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句