c++multithreadingthread-safetyatomicsmart-pointers

Atomic operations on `unique_ptr`


std::shared_ptr has specializations for atomic operations like atomic_compare_exchange_weak and family, but I cannot find documentation on equivalent specializations for std::unique_ptr. Are there any? If not, why not?


Solution

  • No there are no standard atomic functions for std::unique_ptr.

    I did find an argument for why not in Atomic Smart Pointers(N4058) by Herb Sutter

    Lawrence Crowl responded to add:

    One of the reasons that shared_ptr locking is the way it is is to avoid a situation in which we weaken the precondition on the atomic template parameter that it be trivial, and hence have no risk of deadlock.

    That said, we could weaken the requirement so that the argument type only needs to be lockfree, or perhaps only non-recursively locking.

    However, while trivial makes for reasonably testable traits, I see no effective mechanism to test for the weaker property.

    That proposal has been assigned to the Concurrency Subgroup and has no disposition as of yet. You can check the status at JTC1/SC22/WG21 - Papers 2014 mailing2014-07