Boost logo

Boost :

From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2021-09-21 14:34:02


Hi Everyone,
I am looking at an example implementation of an asynchronous TCP echo
server in Boost.ASIO library:
https://www.boost.org/doc/libs/1_77_0/doc/html/boost_asio/example/cpp11/echo/async_tcp_echo_server.cpp

It uses a shared_ptr in the implementation to store the things related to
session: socket handle, data buffers. I tried to modify it in a number of
ways to avoid the usage of shared_ptr (and any form of shared ownership),
but they all failed. It looks like I have to use a shared_pointer (or
equivalent) when I use ASIO sockets.

But conceptually, there is never a point in time where two tasks have to
access members of this `session` concurrently: they are always sequenced.
So, at least conceptually, there should be a way to do this with a session
type that is only movable, with unique ownership: a task does its job with
the session and then moves it to the subsequent task. So is this necessity
to use a shared pointer an unnecessary and suboptimal design choice? Or is
there something fundamental that prevents the unique ownership of buffers
and socket handles? Or maybe is there a way to do it in ASIO that I
overlooked?

I am not trying to solve any specific problem. It just strikes me that a
shared_ptr is used in a demo example for the library. I was always of the
opinion that a shared_ptr is often abused as a way of doing an ad-hoc GC.

Regards,
&rzej;


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk