Why use shared_ptr in the first place? You already use the promise/future protocol as the thread safe communication channel between the producer and consumer. I would consider getting a shared_future from the future and copy instances of it for each threads that would want to get the value.

Julien V.


On December 11, 2023 5:23:23 p.m. EST, Andrey Semashev via Std-Proposals <std-proposals@lists.isocpp.org> wrote:
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@lists.isocpp.org> 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.