Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::asio::deadline_timer destruction
From: Igor R (boost.lists_at_[hidden])
Date: 2013-04-19 16:09:19


<...>
>> 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_guard<boost::recursive_mutex> lock(wait_handler_lock);
> handler(ec);
> expired = true;
> wait_handler_done.notify_all();
> }
> public:
<...>
> ~graceful_deadline_timer()
> {
> boost::unique_lock<boost::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_guard<boost::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_deadline_timer.html)


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net