Title
the specification of ~shared_ptr is MT-unfriendly, makes implementation assumptions
Status
cd1
Section
[util.smartptr.shared.dest][tr.util.smartptr.shared.dest]
Submitter
Peter Dimov

Created on 2006-04-23.00:00:00 last changed 171 months ago

Messages

Date: 2010-10-21.18:28:33

Proposed resolution:

Change the first two bullets of [lib.util.smartptr.shared.dest]/1 to:

  • If *this is empty or shares ownership with another shared_ptr instance (use_count() > 1), there are no side effects.
  • If *this shares ownership with another shared_ptr instance (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 *this decreases the number of instances in *this's ownership group by one, all shared_ptr instances that share ownership with *this will report an use_count() that is one lower than its previous value after *this is destroyed. --end note]

Date: 2006-04-23.00:00:00

[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 a shared_ptr instance (because of the "that instance's use count".)

This is contrary to the spirit of the rest of the specification that carefully avoids to require an use count variable. Instead, use_count() is specified to return a value, a number of instances.

In multithreaded code, the usual implicit assumption is that a shared variable should not be accessed by more than one thread without explicit synchronization, and by introducing the concept of an "use count" variable, the current wording implies that two shared_ptr instances that share ownership cannot be destroyed simultaneously.

In addition, if we allow the interpretation that an use count variable is part of shared_ptr's state, this would lead to other undesirable consequences WRT multiple threads. For example,

p1 = p2;

would now visibly modify the state of p2, a "write" operation, requiring a lock.

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg3100
2006-04-23 00:00:00admincreate