std::atomic_init(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

okay so bestie, this is std::atomic_init(3) from the C++ Standard Library β€” and yes, the manual name is literally std::atomic_init β€” std::atomic_init, iconic, no notes. πŸ’…


✨ Synopsis ✨

Defined in header <atomic>, and here’s the whole signature situation. Heads up: both overloads are (deprecated in C++20) 😭

Defined in header <atomic>
template< class T >
void atomic_init( std::atomic<T>* obj,
                  typename std::atomic<T>::value_type desired ) noexcept;

template< class T >
void atomic_init( volatile std::atomic<T>* obj,
                  typename std::atomic<T>::value_type desired ) noexcept;
  • (1) β€” the std::atomic<T>* obj overload; it’s (since C++11) and (deprecated in C++20).
  • (2) β€” the volatile std::atomic<T>* obj overload; also (since C++11) and (deprecated in C++20).

So what does it do? It initializes the default-constructed atomic object obj with the value desired. Slay. ✨

🚨 This function is not atomic. Concurrent access from another thread β€” even through an atomic operation β€” is a data race. Not a cute look, bestie. And if obj was not default-constructed, or if this function is called twice on the same obj, the behavior is undefined. Like, actually undefined. πŸ™ˆ

πŸ¦‹ Parameters πŸ¦‹

  • obj β€” pointer to an atomic object to initialize.
  • desired β€” the value to initialize atomic object with.

πŸŽ€ Return value πŸŽ€

  • (none) β€” literally nothing comes back. πŸ’–

🌈 Notes 🌈

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


🧚 Example 🧚

This section is incomplete Reason: no example

not me crying at the missing example 😭


πŸ’€ Defect reports πŸ’€

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

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

πŸ‘‘ See also πŸ‘‘

  • ATOMIC_VAR_INIT β€” constant initialization of an atomic variable of static storage duration. It’s a function macro, (C++11), (deprecated in C++20).
  • constructor β€” constructs an atomic object; it’s a public member function of std::atomic<T>.
  • C documentation for atomic_init β€” yes, C has its own docs for this one.

πŸ“ Category πŸ“

  • * Todo no example

Kisses and good vibes, your C++ bestie πŸ’– β€” source/version: http://cppreference.com, date: 2024.06.10, manual page: std::atomic_init(3). Periodt. 🌟

πŸ“¦ where this page comes from