Boost logo

Boost Users :

From: Vinnie Falco (vinnie.falco_at_[hidden])
Date: 2019-08-12 17:01:42


On Mon, Aug 12, 2019 at 8:57 AM TomJordan via Boost-users
<boost-users_at_[hidden]> wrote:
> Is this shared_ptr approach the idiomatic or even the only way to
> manage lifetimes in ASIO async calls?

It is not the only way, but by far the simplest and safest - once you
achieve a grasp of how asynchrony works and how to manage lifetimes.

> It just looks a bit ugly to me, bloats the code a bit and I was wondering if there is
> any other way to do it without the intrusion of shared_ptrs into the mix?

My advice: get over it. There is nothing wrong with `shared_ptr` and
it is the most natural way to manage objects whose lifetime is not
deterministic. For example if you perform an asynchronous read and an
asynchronous write with the same object, there's no way to know which
operation will finish last. Shared-ownership (i.e. `shared_ptr`) is
the right tool here.

This video and the example code that is used in the video should give
you a running start on learning asio and asynchronous programming. It
explains how to use shared_ptr in detail to manage lifetimes.

<https://www.youtube.com/watch?v=7FQwAjELMek>

Added bonus: after watching the video, you might quickly get rich.

Regards


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