Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::atomic<std::shared_ptr>

      From cppreference.com
      <cpp‎ |memory‎ |shared ptr
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
      std::shared_ptr
      Member functions
      Modifiers
      Observers
      (until C++20*)
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      functions(until C++26*)
      Helper classes
      atomic<std::shared_ptr>
      (C++20)
      Deduction guides(C++17)
       
      Defined in header<memory>
      template<class T>
      structstd::atomic<std::shared_ptr<T>>;
      (since C++20)

      The partial template specialization ofstd::atomic forstd::shared_ptr<T> allows users to manipulateshared_ptr objects atomically.

      If multiple threads of execution access the samestd::shared_ptr object without synchronization and any of those accesses uses a non-const member function ofshared_ptr then a data race will occur unless all such access is performed through an instance ofstd::atomic<std::shared_ptr> (or, deprecated as of C++20, through thestandalone functions for atomic access tostd::shared_ptr).

      Associateduse_count increments are guaranteed to be part of the atomic operation. Associateduse_count decrements are sequenced after the atomic operation, but are not required to be part of it, except for theuse_count change when overridingexpected in a failed CAS. Any associated deletion and deallocation are sequenced after the atomic update step and are not part of the atomic operation.

      Note that the control block of ashared_ptr is thread-safe: different non-atomicstd::shared_ptr objects can be accessed using mutable operations, such asoperator= orreset, simultaneously by multiple threads, even when these instances are copies, and share the same control block internally.

      The type T may be an incomplete type.

      Contents

      [edit]Member types

      Member type Definition
      value_typestd::shared_ptr<T>

      [edit]Member functions

      All non-specializedstd::atomic functions are also provided by this specialization, and no additional member functions.

      atomic<shared_ptr<T>>::atomic

      constexpr atomic()noexcept=default;
      (1)
      constexpr atomic(std::nullptr_t)noexcept: atomic(){}
      (2)
      atomic(std::shared_ptr<T> desired)noexcept;
      (3)
      atomic(const atomic&)= delete;
      (4)
      1,2) Initializes the underlyingshared_ptr<T> to the null value.
      3) Initializes the underlyingshared_ptr<T> to a copy ofdesired. As with anystd::atomic type, initialization is not an atomic operation.
      4) Atomic types are not copy/move constructible.

      atomic<shared_ptr<T>>::operator=

      void operator=(const atomic&)= delete;
      (1)
      void operator=(std::shared_ptr<T> desired)noexcept;
      (2)
      void operator=(std::nullptr_t)noexcept;
      (3)
      1) Atomic types are not copy/move assignable.
      2) Value assignment, equivalent tostore(desired).
      3) Resets the atomic shared pointer to null pointer value. Equivalent tostore(nullptr);.

      atomic<shared_ptr<T>>::is_lock_free

      bool is_lock_free()constnoexcept;

      Returnstrue if the atomic operations on all objects of this type are lock-free,false otherwise.

      atomic<shared_ptr<T>>::store

      void store(std::shared_ptr<T> desired,
                 std::memory_order order=std::memory_order_seq_cst)noexcept;

      Atomically replaces the value of*this with the value ofdesired as if byp.swap(desired) wherep is the underlyingstd::shared_ptr<T>. Memory is ordered according toorder. The behavior is undefined iforder isstd::memory_order_consume,std::memory_order_acquire, orstd::memory_order_acq_rel.

      atomic<shared_ptr<T>>::load

      Atomically returns a copy of the underlying shared pointer. Memory is ordered according toorder. The behavior is undefined iforder isstd::memory_order_release orstd::memory_order_acq_rel.

      atomic<shared_ptr<T>>::operator std::shared_ptr<T>

      operatorstd::shared_ptr<T>()constnoexcept;

      Equivalent toreturn load();.

      atomic<shared_ptr<T>>::exchange

      std::shared_ptr<T> exchange(std::shared_ptr<T> desired,
                                   std::memory_order order=std::memory_order_seq_cst)noexcept;

      Atomically replaces the underlyingstd::shared_ptr<T> withdesired as if byp.swap(desired) wherep is the underlyingstd::shared_ptr<T> and returns a copy of the value thatp had immediately before the swap. Memory is ordered according toorder. This is an atomic read-modify-write operation.

      atomic<shared_ptr<T>>::compare_exchange_weak, compare_exchange_strong

      bool compare_exchange_strong(std::shared_ptr<T>& expected,std::shared_ptr<T> desired,
                                   std::memory_order success,std::memory_order failure)noexcept;
      (1)
      bool compare_exchange_weak(std::shared_ptr<T>& expected,std::shared_ptr<T> desired,
                                 std::memory_order success,std::memory_order failure)noexcept;
      (2)
      bool compare_exchange_strong(std::shared_ptr<T>& expected,std::shared_ptr<T> desired,
                                   std::memory_order order=std::memory_order_seq_cst)noexcept;
      (3)
      bool compare_exchange_weak(std::shared_ptr<T>& expected,std::shared_ptr<T> desired,
                                 std::memory_order order=std::memory_order_seq_cst)noexcept;
      (4)
      1) If the underlyingstd::shared_ptr<T> stores the sameT* asexpected and shares ownership with it, or if both underlying andexpected are empty, assigns fromdesired to the underlyingstd::shared_ptr<T>, returnstrue, and orders memory according tosuccess, otherwise assigns from the underlyingstd::shared_ptr<T> toexpected, returnsfalse, and orders memory according tofailure. The behavior is undefined iffailure isstd::memory_order_release orstd::memory_order_acq_rel. On success, the operation is an atomic read-modify-write operation on*this andexpected is not accessed after the atomic update. On failure, the operation is an atomic load operation on*this andexpected is updated with the existing value read from the atomic object. This update toexpected'suse_count is part of this atomic operation, although the write itself (and any subsequent deallocation/destruction) is not required to be.
      2) Same as(1), but may also fail spuriously.
      3) Equivalent to:return compare_exchange_strong(expected, desired, order, fail_order);, wherefail_order is the same asorder except thatstd::memory_order_acq_rel is replaced bystd::memory_order_acquire andstd::memory_order_release is replaced bystd::memory_order_relaxed.
      4) Equivalent to:return compare_exchange_weak(expected, desired, order, fail_order);, wherefail_order is the same asorder except thatstd::memory_order_acq_rel is replaced bystd::memory_order_acquire andstd::memory_order_release is replaced bystd::memory_order_relaxed.

      atomic<shared_ptr<T>>::wait

      void wait(std::shared_ptr<T> old,
                 std::memory_order order=std::memory_order_seq_cst)constnoexcept;

      Performs an atomic waiting operation.

      Comparesload(order) withold and if they are equivalent then blocks until*this is notified bynotify_one() ornotify_all(). This is repeated untilload(order) changes. This function is guaranteed to return only if value has changed, even if underlying implementation unblocks spuriously.

      Memory is ordered according toorder. The behavior is undefined iforder isstd::memory_order_release orstd::memory_order_acq_rel.

      Notes: twoshared_ptrs are equivalent if they store the same pointer and either share ownership or are both empty.

      atomic<shared_ptr<T>>::notify_one

      void notify_one()noexcept;

      Performs an atomic notifying operation.

      If there is a thread blocked in atomic waiting operations (i.e.wait()) on*this, then unblocks at least one such thread; otherwise does nothing.

      atomic<shared_ptr<T>>::notify_all

      void notify_all()noexcept;

      Performs an atomic notifying operation.

      Unblocks all threads blocked in atomic waiting operations (i.e.wait()) on*this, if there are any; otherwise does nothing.

      [edit]Member constants

      The only standardstd::atomic member constantis_always_lock_free is also provided by this specialization.

      atomic<shared_ptr<T>>::is_always_lock_free

      staticconstexprbool is_always_lock_free=/*implementation-defined*/;

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_atomic_shared_ptr201711L(C++20)std::atomic<std::shared_ptr>

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3661C++20atomic<shared_ptr<T>> was not constant-initializable fromnullptrmade constant-initializable
      LWG 3893C++20LWG3661 madeatomic<shared_ptr<T>> not assignable fromnullptr_tassignability restored

      [edit]See also

      (C++11)
      atomic class template and specializations for bool, integral, floating-point,(since C++20) and pointer types
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/atomic2&oldid=155417"

      [8]ページ先頭

      ©2009-2025 Movatter.jp