Home > Mobile >  Can the boost::asio timer object be deleted before the corresponding callback is called?
Can the boost::asio timer object be deleted before the corresponding callback is called?

Time:08-13

Should the timer object exist until the task is completed?

I mean the following:

boost::asio::io_service io;

{
    boost::asio::steady_timer timer(io, std::chrono::seconds(5));
    timer.async_wait(someCallback);
} // the timer object is deleted here

io.run();

Is this allowed and does it lead to undefined behavior?

CodePudding user response:

The destructor simply cancels any pending waits so your callback will be called with the boost::asio::error::operation_aborted error code.

  • Related