2810. use_count and unique in shared_ptr

Section: 23.11.3 [util.smartptr.shared] Status: Resolved Submitter: Marshall Clow Opened: 2016-11-09 Last modified: 2017-02-02

Priority: Not Prioritized

View other active issues in [util.smartptr.shared].

View all other issues in [util.smartptr.shared].

View all issues with Resolved status.

Discussion:

Addresses CA 14

The removal of the "debug only" restriction for use_count() and unique() in shared_ptr introduced a bug: in order for unique() to produce a useful and reliable value, it needs a synchronize clause to ensure that prior accesses through another reference are visible to the successful caller of unique(). Many current implementations use a relaxed load, and do not provide this guarantee, since it's not stated in the Standard. For debug/hint usage that was OK. Without it the specification is unclear and misleading.

Proposed change:

A solution could make unique() use memory_order_acquire, and specifying that reference count decrement operations synchronize with unique(). This won't provide sequential consistency but may be useful. We could also specify use_count() as only providing an unreliable hint of the actual count, or deprecate it.

[Issues Telecon 16-Dec-2016]

Resolved by adoption of P0521R0.

Proposed resolution: