C++ Logo

std-proposals

Advanced search

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

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Tue, 12 Dec 2023 01:23:23 +0300
On 12/11/23 22:42, Marcin Jaczewski via Std-Proposals wrote:
> pon., 11 gru 2023 o 18:26 Thiago Macieira via Std-Proposals
> <std-proposals_at_[hidden]> napisaƂ(a):
>>
>> On Monday, 11 December 2023 08:45:31 PST Valentin Palade wrote:
>>> 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?
>>
>> No. It can be between 1 and 5.
>>
>> But if it's 1, then you have the only copy and there is no other thread seeing
>> this object. The number can't change unless the current thread changes it.
>
> But all threads can return 5 too. As check is done before decrement
> every one can observe the state before it.
> And this means no thread can get 1 and acquire the last instance.
> We need to guarantee that one thread will be the least instance.

Yes, but the same can happen with the proposed resurrect() method as well.

If this is a concern then a better solution is to use a custom deleter
that will pass the object to the producer instead of deleting it.

Received on 2023-12-11 22:23:26