
<...>
void f(const system::error_code&, const deadline_timer *t) { delete t; // to wait or not to wait? }
int main() { io_service io; deadline_timer *t = new deadline_timer(io); t->expires_from_now(seconds(10)); t->async_wait(boost::bind(&f, _1, t)); io.run(); }
I don't understand why did you delete object from wait handler...
Just to demonstrate a trivial use-case where your idea won't work.
I meant a little bit different thing like this: class graceful_deadline_timer : public boost::asio::deadline_timer { <...> void handler_wrapper(const boost::system::error_code &ec) { boost::lock_guardboost::recursive_mutex lock(wait_handler_lock); handler(ec); expired = true; wait_handler_done.notify_all(); } public: <...> ~graceful_deadline_timer() { boost::unique_lockboost::recursive_mutex lock(wait_handler_lock); if(!expired) { cancel(); while(!expired) { wait_handler_done.wait(lock); } } }
template <typename WaitHandler> void async_wait(WaitHandler _handler) { boost::lock_guardboost::recursive_mutex lock(wait_handler_lock); handler = _handler; expired = false; super::async_wait(boost::bind(&graceful_deadline_timer::handler_wrapper, this, boost::asio::placeholders::error)); } };
Ok, now please apply your code to the case I mentioned before. In particular, what the following code would do:
while(!expired) { wait_handler_done.wait(lock); }
Seems like a deadlock, doesn't it? (And again, all these locks in async_wait/d-tor/handler imply that you intend to access deadline_timer from multiple threads. Note that it's not safe as deadline_timer is not thread-safe: http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/basic_dea...)