Date: Mon, 11 Dec 2023 20:03:45 +0300
On 12/11/23 19:45, Valentin Palade via Std-Proposals wrote:
> On Mon, Dec 11, 2023 at 5:57 PM Thiago Macieira via Std-Proposals
> <std-proposals_at_[hidden] <mailto: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?
use_count() can only return 1 if there is only one shared_ptr pointing
to the object. How is it possible that multiple threads are using the
same object and yet use_count() returns 1? Assuming no UB and incorrect
usage like caching somewhere a raw pointer to the object instead of
shared_ptr, of course.
> On Mon, Dec 11, 2023 at 5:57 PM Thiago Macieira via Std-Proposals
> <std-proposals_at_[hidden] <mailto: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?
use_count() can only return 1 if there is only one shared_ptr pointing
to the object. How is it possible that multiple threads are using the
same object and yet use_count() returns 1? Assuming no UB and incorrect
usage like caching somewhere a raw pointer to the object instead of
shared_ptr, of course.
Received on 2023-12-11 17:03:49