Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      ATOMIC_VAR_INIT

      From cppreference.com
      <cpp‎ |atomic
       
       
      Concurrency support library
      Threads
      (C++11)
      (C++20)
      this_thread namespace
      (C++11)
      (C++11)
      (C++11)
      Cooperative cancellation
      Mutual exclusion
      Generic lock management
      (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)
      Safe reclamation
      Hazard pointers
      Atomic types
      (C++11)
      (C++20)
      Initialization of atomic types
      (C++11)(deprecated in C++20)
      ATOMIC_VAR_INIT
      (C++11)(deprecated in C++20)
      Memory ordering
      (C++11)(deprecated in C++26)
      Free functions for atomic operations
      Free functions for atomic flags
       
      Defined in header<atomic>
      #define ATOMIC_VAR_INIT(value) /* implementation-defined */
      (since C++11)
      (deprecated in C++20)

      Expands to an expression which can be used to initialize anstd::atomic object that can be initialized fromvalue. If the atomic object has static storage duration, this initialization isconstant initialization.

      [edit]Notes

      Accessing the variable during initialization from another thread, even through an atomic operation, is a data race (it may happen if the address is immediately passed to another thread with astd::memory_order_relaxed operation)

      This macro is primarily provided for compatibility with C; it behaves the same as the constructor ofstd::atomic.

      [edit]Example

      Run this code
      #include <atomic>#include <iostream> int main(){std::atomic<int> a= ATOMIC_VAR_INIT(1);// std::atomic<int> a(1);   // C++-only alternativestd::cout<<"Initialized std::atomic<int> as: "<< a<<'\n';}

      Output:

      Initialized std::atomic<int> as: 1

      [edit]See also

      (C++11)(deprecated in C++20)
      non-atomic initialization of a default-constructed atomic object
      (function template)[edit]
      constructs an atomic object
      (public member function ofstd::atomic<T>)[edit]
      C documentation forATOMIC_VAR_INIT
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/atomic/ATOMIC_VAR_INIT&oldid=138654"

      [8]ページ先頭

      ©2009-2025 Movatter.jp