Boost logo

Boost Users :

Subject: [Boost-users] ASIO cancel question
From: J.W.F. Thirion (derik_at_[hidden])
Date: 2009-08-06 10:16:59


Dear Boost community,

In ASIO, when I execute a cancel() on a tcp::socket, does it remove all
of the handlers associated with the socket from the handler queue?

In other words, let's say I've started an async read and write
operation. Assuming the read completed and a handleRead is placed on the
internal handler queue but have not yet been called by the IO service
thread. The write operation is still in progress. Let's assume now that
the object destructs before the handleRead is called, but in the
destructor a cancel operation is done on the socket to cancel any
outstanding asynchronous requests (e.g. in the destructor of the class).
The way I've got it is that the handleWrite will be called with an
aborted/cancelled error, and the handleRead will be removed from the
handler queue. Is this correct?

Also, as reading and writing operations are done in the IO service
thread, would it be safe to call the tcp::socket cancel from another
thread (e.g. in the destructor of a class)?

Another problem then occurs: The destructor could be entered, just as
the handler function is entered (e.g. with success as error code), and
the object destroyed. Accessing of the member variables will then cause
a crash of the program. Any ideas on how to get around this problem of
the object destructing but the handler function already called by the IO
service thread? All I can think is that one posts the deletion of the
object onto the same handler queue, do nothing in the destructor of the
object, override the constructor and destructor of the object to be
private so that a getObject and destroyObject function news and posts a
delete for the object.

Any ideas would be highly appreciated.

Thanks!
Derik


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