C++ Logo


Advanced search

Re: [std-proposals] std::shared_ptr resurrect

From: Valentin Palade <vipalade_at_[hidden]>
Date: Mon, 11 Dec 2023 18:45:31 +0200
On Mon, Dec 11, 2023 at 5:57 PM Thiago Macieira via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Monday, 11 December 2023 07:46:00 PST Valentin Palade via Std-Proposals
> wrote:
> > std::shared_ptr<T> sm2(std::move(sm));
> > will preserve the use count in the control block attached to the object
> > referenced by sm so the if (sm2.use_count()) will be similar to if
> > (sm.use_count()).
> >
> > The idea is that the validation that we are the last instance
> referencing a
> > certain object, must be made atomically
> This is an equivalent check, but more expensive. Jonathan's suggestion is
> faster and has the benefit of working since C++11, with no additional API.
> I would even argue that you should already have a local stack object in
> the
> first place in your design, not referring to some globally-visible
> std::shared_ptr variable. You won't need to add more code.

Please note that sm is not a global shared_ptr instance - there will be an
sm instance for each consumer lambda instance - in our case 4 instances,
one for each thread, each pointing to the same object with a possible max
use_count == 5 (one for the instance in producer).

Doing a local copy as Jonathan suggests, will make no difference, with
threads running concurrently, we can still have use_count() return anything
between 1 and 5.

Or am I missing something?

Received on 2023-12-11 16:45:43