Igor R
2014-01-11 19:35:43 UTC
In the multi-threaded HTTP Server 3 example, the author uses the
new_connection_.reset(new connection(io_service_, request_handler_));
Here, new_connection_ is a shared pointer of a TCP/IP connection. The
way I understand shared pointers and the reset() member function, the
previous connection destructor will be called (i.e. the connection is
shut down) before a new connection is created and assigned to the
shared pointer. This would not create a multi-threaded HTTP server; if
it does, I do not understand how. Can someone please explain this
construct to me?
Take a look at connection::start() member-function, which is callednew_connection_.reset(new connection(io_service_, request_handler_));
Here, new_connection_ is a shared pointer of a TCP/IP connection. The
way I understand shared pointers and the reset() member function, the
previous connection destructor will be called (i.e. the connection is
shut down) before a new connection is created and assigned to the
shared pointer. This would not create a multi-threaded HTTP server; if
it does, I do not understand how. Can someone please explain this
construct to me?
from handle_accept(). It launches a chain of async operations
(async_read_some-->handle_read-->async_read/write-->etc), and each of
them uses "shared_from_this" approach to manage the connection object
lifespan. So, in start_accept() it's already safe to reset the
new_connection_ -- moreover, it's necessary to avoid one superfluous
reference.