Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | atomic
(P0883R2)
(Splitted the volatile overloads.)
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
 
{{cpp/title|atomic_init}}
 
{{cpp/title|atomic_init}}
{{cpp/atomic/navbar}}
+
{{cpp/thread/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | atomic}}
+
{{dcl header|atomic}}
{{dcl | since=c++11 | deprecated=c++20 |
+
{{dcl|num=1|since=c++11|deprecated=c++20|
 
template< class T >
 
template< class T >
void atomic_init( std::atomic<T>* obj,
+
void atomic_init
                  typename std::atomic<T>::value_type desired ) noexcept;
+
    ( std::atomic<T>* obj,
 +
      typename std::atomic<T>::value_type desired ) noexcept;
 
}}
 
}}
{{dcl | since=c++11 | deprecated=c++20 |
+
{{dcl|num=2|since=c++11|deprecated=c++20|
 
template< class T >
 
template< class T >
void atomic_init( volatile std::atomic<T>* obj,
+
void atomic_init
                  typename std::atomic<T>::value_type desired ) noexcept;
+
    ( volatile std::atomic<T>* obj,
 +
      typename std::atomic<T>::value_type desired ) noexcept;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Initializes the default-constructed atomic object {{tt|obj}} with the value {{tt|desired}}. The function is not atomic: concurrent access from another thread, even through an atomic operation, is a data race.  
+
Initializes the default-constructed atomic object {{c|obj}} with the value {{c|desired}}. The function is not atomic: concurrent access from another thread, even through an atomic operation, is a data race.  
  
If {{tt|obj}} was not default-constructed, the behavior is undefined.
+
If {{c|obj}} was not default-constructed, or this function is called twice on the same {{c|obj}}, the behavior is undefined.
 
+
If this function is called twice on the same {{tt|obj}}, the behavior is undefined.
+
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | obj | pointer to an atomic object to initialize}}
+
{{par|obj|pointer to an atomic object to initialize}}
{{par | desired | the value to initialize atomic object with}}
+
{{par|desired|the value to initialize atomic object with}}
 
{{par end}}
 
{{par end}}
  
Line 34: Line 34:
  
 
===Example===
 
===Example===
{{example
+
{{example}}
|
+
| code=
+
| output=
+
}}
+
  
 
===Defect reports===
 
===Defect reports===
 
{{dr list begin}}
 
{{dr list begin}}
{{dr list item|std=C++11|paper=P0558R1|before=exact type match required because {{tt|T}} is deduced from multiple arguments|after={{tt|T}} is deduced from the {{tt|atomic}} argument only}}
+
{{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}}
 
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/atomic/dsc ATOMIC_VAR_INIT}}
+
{{dsc inc|cpp/atomic/dsc ATOMIC_VAR_INIT}}
{{dsc inc | cpp/atomic/atomic/dsc constructor}}
+
{{dsc inc|cpp/atomic/atomic/dsc constructor}}
{{dsc see c | c/atomic/atomic_init}}
+
{{dsc see c|c/atomic/atomic_init}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 22:45, 20 September 2023

 
 
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
atomic_init
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
Free functions for atomic flags
 
Defined in header <atomic>
template< class T >

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

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

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

      typename std::atomic<T>::value_type desired ) noexcept;
(2) (since C++11)
(deprecated in C++20)

Initializes the default-constructed atomic object obj with the value desired. The function is not atomic: concurrent access from another thread, even through an atomic operation, is a data race.

If obj was not default-constructed, or this function is called twice on the same obj, the behavior is undefined.

Contents

[edit] Parameters

obj - pointer to an atomic object to initialize
desired - the value to initialize atomic object with

[edit] Return value

(none)

[edit] Notes

This function is provided for compatibility with C. If the compatibility is not required, std::atomic may be initialized through their non-default constructors.

[edit] Example

[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

(C++11)(deprecated in C++20)
constant initialization of an atomic variable of static storage duration
(function macro) [edit]
constructs an atomic object
(public member function of std::atomic<T>) [edit]
C documentation for atomic_init