This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofCD1 status.
Section: 20.3.2.2.3[util.smartptr.shared.dest], 99 [tr.util.smartptr.shared.dest]Status:CD1Submitter: Peter DimovOpened: 2006-04-23Last modified: 2016-01-28
Priority:Not Prioritized
View all otherissues in [util.smartptr.shared.dest].
View all issues withCD1 status.
Discussion:
[tr.util.smartptr.shared.dest] says in its second bullet:
"If *this shares ownership with another shared_ptr instance (use_count() > 1),decrements that instance's use count."
The problem with this formulation is that it presupposes the existence of an"use count" variable that can be decremented and that is part of the state of ashared_ptr instance (because of the "that instance's use count".)
This is contrary to the spirit of the rest of the specification that carefullyavoids to require an use count variable. Instead, use_count() is specified toreturn a value, a number of instances.
In multithreaded code, the usual implicit assumption is that a shared variableshould not be accessed by more than one thread without explicit synchronization,and by introducing the concept of an "use count" variable, the current wordingimplies that two shared_ptr instances that share ownership cannot be destroyedsimultaneously.
In addition, if we allow the interpretation that an use count variable is partof shared_ptr's state, this would lead to other undesirable consequences WRTmultiple threads. For example,
p1 = p2;
would now visibly modify the state of p2, a "write" operation, requiring a lock.
Proposed resolution:
Change the first two bullets of [lib.util.smartptr.shared.dest]/1 to:
- If
*thisisemptyor shares ownership with anothershared_ptrinstance (use_count() > 1), there are no side effects.If*thisshares ownership with anothershared_ptrinstance(use_count() > 1), decrements that instance's use count.
Add the following paragraph after [lib.util.smartptr.shared.dest]/1:
[Note: since the destruction of
*thisdecreases the number of instances in*this's ownership group by one, allshared_ptrinstances that share ownershipwith*thiswill report anuse_count()that is one lower than its previous valueafter*thisis destroyed.--end note]