Discussion:
[asio-users] [ASIO] example clarification (how to)
Gaetano Mendola
2013-12-24 20:30:58 UTC
Permalink
I have posted this in the devel mailing list but may be this is the most
appropiate one. Sorry for the double post.

In the asio chat example

http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp11/chat/chat_server.cpp

the class chat_server implements the async_accept handler with a lambda
without use the shared_from_this pattern, isn't an error destroy the
class while the handler is running? Maybe is not a problem looking at
how is used inside the main but without having knowledge on how is it
going to be used which can be the best way to destroy the chat_server
(or similar code) safely? Is something like the following correct?

chat_server::~chat_server() {
//Call dispatching a cancel operation
//in order to be sure to not chain another async_accept
acceptor_.get_service().dispatch(
[this](){
acceptor_.cancel();
});
}

here I'm supposing a single thread is running io_service.run() otherwise
the strand has to be used.


Regards
Gaetano Mendola

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com
Marat Abrarov
2013-12-25 06:21:46 UTC
Permalink
Hi, Gaetano.
Post by Gaetano Mendola
the class chat_server implements the async_accept handler with a lambda
without use the shared_from_this pattern, isn't an error destroy the class
while the handler is running? Maybe is not a problem looking at how is
used inside the main
Yes. The chat_server interface and implementation are related to the way the
class is used in function main. It is example and its author wants it to be
short (but this gives some questions about resource management to the
readers of this example).
Post by Gaetano Mendola
but without having knowledge on how is it going to be
used which can be the best way to destroy the chat_server (or similar
code) safely? Is something like the following correct?
chat_server::~chat_server() {
//Call dispatching a cancel operation
//in order to be sure to not chain another async_accept
acceptor_.get_service().dispatch(
[this](){
acceptor_.cancel();
});
}
The suggested solution has problems with resource management (the lifetime
of chat_server data members). It can be fixed with
shared_ptr/shared_from_this() - but this will make the example larger. The
cancellation of async_accept doesn't help too much (I suggest that
acceptor_.close(ignored_error_code) is better) here.
Post by Gaetano Mendola
here I'm supposing a single thread is running io_service.run() otherwise
the strand has to be used.
Almost every solution has some suppose about (the rules of, the range of)
its usage.

Regards,
Marat Abrarov.
Gaetano Mendola
2013-12-25 19:55:37 UTC
Permalink
Post by Marat Abrarov
Post by Gaetano Mendola
but without having knowledge on how is it going to be
used which can be the best way to destroy the chat_server (or similar
code) safely? Is something like the following correct?
chat_server::~chat_server() {
//Call dispatching a cancel operation
//in order to be sure to not chain another async_accept
acceptor_.get_service().dispatch(
[this](){
acceptor_.cancel();
});
}
The suggested solution has problems with resource management (the lifetime
of chat_server data members). It can be fixed with
shared_ptr/shared_from_this() - but this will make the example
larger. The
Post by Marat Abrarov
cancellation of async_accept doesn't help too much (I suggest that
acceptor_.close(ignored_error_code) is better) here.
Isn't the io_service::dispatch a blocking call? That was my attempt to
interrupt outside the ioservice loop the async call chain.

If it's not what's the best way to interrupt an async calls chain then?
Loading...