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)
      atomic_xxx
      functions(until C++26*)
      Helper classes
      Deduction guides(C++17)
       
      Defined in header<memory>
      template<class T>
      bool atomic_is_lock_free(conststd::shared_ptr<T>* p);
      (1)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>
      std::shared_ptr<T> atomic_load(conststd::shared_ptr<T>* p);
      (2)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>

      std::shared_ptr<T> atomic_load_explicit

         (conststd::shared_ptr<T>* p,std::memory_order mo);
      (3)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>
      void atomic_store(std::shared_ptr<T>* p,std::shared_ptr<T> r);
      (4)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>

      void atomic_store_explicit
         (std::shared_ptr<T>* p,std::shared_ptr<T> r,

           std::memory_order mo);
      (5)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>

      std::shared_ptr<T> atomic_exchange

         (std::shared_ptr<T>* p,std::shared_ptr<T> r);
      (6)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>

      std::shared_ptr<T> atomic_exchange_explicit
         (std::shared_ptr<T>* p,std::shared_ptr<T> r,

           std::memory_order mo);
      (7)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>

      bool atomic_compare_exchange_weak
         (std::shared_ptr<T>* p,std::shared_ptr<T>* expected,

           std::shared_ptr<T> desired);
      (8)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>

      bool atomic_compare_exchange_strong
         (std::shared_ptr<T>* p,std::shared_ptr<T>* expected,

           std::shared_ptr<T> desired);
      (9)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>

      bool atomic_compare_exchange_strong_explicit
         (std::shared_ptr<T>* p,std::shared_ptr<T>* expected,
           std::shared_ptr<T> desired,

           std::memory_order success,std::memory_order failure);
      (10)(since C++11)
      (deprecated in C++20)
      (removed in C++26)
      template<class T>

      bool atomic_compare_exchange_weak_explicit
         (std::shared_ptr<T>* p,std::shared_ptr<T>* expected,
           std::shared_ptr<T> desired,

           std::memory_order success,std::memory_order failure);
      (11)(since C++11)
      (deprecated in C++20)
      (removed in C++26)

      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 these functions, which are overloads of the corresponding atomic access functions (std::atomic_load,std::atomic_store, etc.).

      Note that the control block of ashared_ptr is thread-safe: differentstd::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.

      1) Determines whether atomic access to the shared pointer pointed-to byp is lock-free.
      2) Equivalent toatomic_load_explicit(p,std::memory_order_seq_cst).
      3) Returns the shared pointer pointed-to byp.
      As with the non-specializedstd::atomic_load_explicit, ifmo isstd::memory_order_release orstd::memory_order_acq_rel, the behavior is undefined.
      4) Equivalent toatomic_store_explicit(p, r,std::memory_order_seq_cst).
      5) Stores the shared pointerr in the shared pointer pointed-to byp atomically,as if byp->swap(r).
      As with the non-specializedstd::atomic_store_explicit, ifmo isstd::memory_order_acquire orstd::memory_order_acq_rel, the behavior is undefined.
      6) Equivalent toatomic_exchange_explicit(p, r,std::memory_order_seq_cst).
      7) Stores the shared pointerr in the shared pointer pointed to byp and returns the value formerly pointed-to byp, atomically,as if byp->swap(r) and returns a copy ofr after the swap.
      8) Equivalent to
      atomic_compare_exchange_weak_explicit
         (p, expected, desired,std::memory_order_seq_cst,
                                 std::memory_order_seq_cst)
      .
      9) Equivalent to
      atomic_compare_exchange_strong_explicit
         (p, expected, desired,std::memory_order_seq_cst,
                                 std::memory_order_seq_cst)
      .
      10,11) Compares the shared pointers pointed-to byp andexpected.
      • If they are equivalent (store the same pointer value, and either share ownership of the same object or are both empty), assignsdesired into*p using the memory ordering constraints specified bysuccess and returnstrue.
      • If they are not equivalent, assigns*p into*expected using the memory ordering constraints specified byfailure and returnsfalse.
      atomic_compare_exchange_weak_explicit may fail spuriously.
      Ifexpected is a null pointer, orfailure isstd::memory_order_release orstd::memory_order_acq_rel, the behavior is undefined.

      Ifp is a null pointer, the behaviors of these functions are all undefined.

      Contents

      [edit]Parameters

      p, expected - a pointer to astd::shared_ptr
      r, desired - astd::shared_ptr
      mo, success, failure - memory ordering selectors of typestd::memory_order

      [edit]Exceptions

      These functions do not throw exceptions.

      [edit]Return value

      1)true if atomic access is implemented using lock-free instructions.
      2,3) A copy of the pointed-to shared pointer.
      4,5) (none)
      6,7) A copy of the formerly pointed-to shared pointer.
      8-11)true if the shared pointers were equivalent and the exchange was performed,false otherwise.

      [edit]Notes

      These functions are typically implemented using mutexes, stored in a global hash table where the pointer value is used as the key.

      TheConcurrency TS offers atomic smart pointer classesatomic_shared_ptr andatomic_weak_ptr as a replacement for the use of these functions.

      These functions were deprecated in favor of the specializations of thestd::atomic template:std::atomic<std::shared_ptr> andstd::atomic<std::weak_ptr>.

      (since C++20)
      (until C++26)

      These functions were removed in favor of the specializations of thestd::atomic template:std::atomic<std::shared_ptr> andstd::atomic<std::weak_ptr>.

      (since C++26)

      [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 2172C++11expected could be a null pointerthe behavior is undefined in this case
      LWG 2980C++11emptyshared_ptrs were never equivalentequivalent if they store the same pointer value

      [edit]See also

      checks if the atomic type's operations are lock-free
      (function template)[edit]
      atomically replaces the value of the atomic object with a non-atomic argument
      (function template)[edit]
      atomically obtains the value stored in an atomic object
      (function template)[edit]
      atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic
      (function template)[edit]
      atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/atomic&oldid=178235"

      [8]ページ先頭

      ©2009-2025 Movatter.jp