Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/atomic/atomic store"

From cppreference.com
< cpp‎ | atomic
m (+shared_ptr)
(Applied P2869R4 (Remove Deprecated shared_ptr Atomic Access APIs from C++26).)
 
(29 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 
{{cpp/title|atomic_store|atomic_store_explicit}}
 
{{cpp/title|atomic_store|atomic_store_explicit}}
{{cpp/atomic/sidebar}}
+
{{cpp/thread/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | atomic }}
+
{{dcl header|atomic}}
{{ddcl list item | num=1 | notes={{mark since c++11}} | 1=
+
{{dcl|num=1|since=c++11|
 
template< class T >
 
template< class T >
void atomic_store( std::atomic<T>* obj, T desr );
+
void atomic_store( std::atomic<T>* obj,
 +
                  typename std::atomic<T>::value_type desired ) noexcept;
 
}}
 
}}
{{ddcl list item | num=2 | notes={{mark since c++11}} | 1=
+
{{dcl|num=2|since=c++11|
 
template< class T >
 
template< class T >
void atomic_store( volatile std::atomic<T>* obj, T desr );
+
void atomic_store( volatile std::atomic<T>* obj,
 +
                  typename std::atomic<T>::value_type desired ) noexcept;
 
}}
 
}}
{{ddcl list item | num=3 | notes={{mark since c++11}} | 1=
+
{{dcl|num=3|since=c++11|
 
template< class T >
 
template< class T >
void atomic_store_explicit( std::atomic<T>* obj, T desr, std::memory_order order);
+
void atomic_store_explicit( std::atomic<T>* obj,
 +
                            typename std::atomic<T>::value_type desired,  
 +
                            std::memory_order order) noexcept;
 
}}
 
}}
{{ddcl list item | num=4 | notes={{mark since c++11}} | 1=
+
{{dcl|num=4|since=c++11|
 
template< class T >
 
template< class T >
void atomic_store_explicit( volatile std::atomic<T>* obj, T desr, std::memory_order order);
+
void atomic_store_explicit( volatile std::atomic<T>* obj,
 +
                            typename std::atomic<T>::value_type desired,  
 +
                            std::memory_order order) noexcept;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
1-2) Atomically replaces the value pointed to by {{tt|obj}} with the value of {{tt|desr}} as if by {{cpp|obj->store(desr)}}
+
@1,2@ Atomically replaces the value pointed to by {{c|obj}} with the value of {{c|desired}} as if by {{c|obj->store(desired)}}.
  
3-4) Atomically replaces the value pointed to by {{tt|obj}} with the value of {{tt|desr}} as if by {{cpp|obj->store(desr, order)}}
+
@3,4@ Atomically replaces the value pointed to by {{c|obj}} with the value of {{c|desired}} as if by {{c|obj->store(desired, order)}}.
 +
@@ If {{c|order}} is one of {{c|std::memory_order_consume}}, {{c|std::memory_order_acquire}} and {{c|std::memory_order_acq_rel}}, the behavior is undefined.
  
 
===Parameters===
 
===Parameters===
 +
{{par begin}}
 +
{{par|obj|pointer to the atomic object to modify}}
 +
{{par|desired|the value to store in the atomic object}}
 +
{{par|order|the memory synchronization ordering}}
 +
{{par end}}
  
{{param list begin}}
+
===Return value===
{{param list item | obj | pointer to the atomic object to modify}}
+
(none)
{{param list item | desr | the value to store in the atomic object}}
+
{{param list item | order | the memory sycnhronization ordering for this operation}}
+
{{param list end}}
+
  
===Exceptions===
+
===Defect reports===
{{noexcept}}
+
{{dr list begin}}
 +
{{dr list item|std=C++11|paper=P0558R1|before=exact type match was required because<br>{{tt|T}} was deduced from multiple arguments|after={{tt|T}} is only deduced<br>from {{c|obj}}}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/atomic/atomic/dcl list store | mem=std::atomic<T>}}
+
{{dsc inc|cpp/atomic/atomic/dsc store|mem=std::atomic<T>}}
{{dcl list template | cpp/atomic/dcl list atomic_load}}
+
{{dsc inc|cpp/atomic/dsc atomic_load}}
{{dcl list template | cpp/atomic/dcl list memory_order}}
+
{{dsc inc|cpp/atomic/dsc memory_order}}
{{dcl list ptfun | cpp/memory/shared_ptr/atomic_access | title=std::atomic_store{{dcl small|(std::shared_ptr)}}
+
{{dsc break}}
<br>std::atomic_store_explicit{{dcl small|(std::shared_ptr)}} | specializes atomic operations for std::shared_ptr }}
+
{{dsc tfun|cpp/memory/shared_ptr/atomic|notes={{mark life|deprecated=c++20|removed=c++26|br=yes}}|title=std::atomic_store{{dsc small|(std::shared_ptr)}}
{{dcl list end}}
+
<br>std::atomic_store_explicit{{dsc small|(std::shared_ptr)}}|specializes atomic operations for {{lc|std::shared_ptr}}}}
 +
{{dsc see c|c/atomic/atomic_store|atomic_store|atomic_store_explicit}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 04:49, 24 April 2024

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
atomic_storeatomic_store_explicit
(C++11)(C++11)
Free functions for atomic flags
 
Defined in header <atomic>
template< class T >

void atomic_store( std::atomic<T>* obj,

                   typename std::atomic<T>::value_type desired ) noexcept;
(1) (since C++11)
template< class T >

void atomic_store( volatile std::atomic<T>* obj,

                   typename std::atomic<T>::value_type desired ) noexcept;
(2) (since C++11)
template< class T >

void atomic_store_explicit( std::atomic<T>* obj,
                            typename std::atomic<T>::value_type desired,

                            std::memory_order order) noexcept;
(3) (since C++11)
template< class T >

void atomic_store_explicit( volatile std::atomic<T>* obj,
                            typename std::atomic<T>::value_type desired,

                            std::memory_order order) noexcept;
(4) (since C++11)
1,2) Atomically replaces the value pointed to by obj with the value of desired as if by obj->store(desired).
3,4) Atomically replaces the value pointed to by obj with the value of desired as if by obj->store(desired, order).
If order is one of std::memory_order_consume, std::memory_order_acquire and std::memory_order_acq_rel, the behavior is undefined.

Contents

[edit] Parameters

obj - pointer to the atomic object to modify
desired - the value to store in the atomic object
order - the memory synchronization ordering

[edit] Return value

(none)

[edit] Defect reports

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

DR Applied to Behavior as published Correct behavior
P0558R1 C++11 exact type match was required because
T was deduced from multiple arguments
T is only deduced
from obj

[edit] See also

atomically replaces the value of the atomic object with a non-atomic argument
(public member function of std::atomic<T>) [edit]
atomically obtains the value stored in an atomic object
(function template) [edit]
defines memory ordering constraints for the given atomic operation
(enum) [edit]
specializes atomic operations for std::shared_ptr
(function template)
C documentation for atomic_store, atomic_store_explicit