Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Atomic types

      From cppreference.com
      <c‎ |language
       
       
       
       

      Contents

      [edit]Syntax

      _Atomic(type-name) (1)(since C11)
      _Atomictype-name (2)(since C11)
      1) Use as a type specifier; this designates a new atomic type
      2) Use as a type qualifier; this designates the atomic version oftype-name. In this role, it may be mixed withconst,volatile, andrestrict, although unlike other qualifiers, the atomic version oftype-name may have a different size, alignment, and object representation.
      type-name - any type other than array or function. For(1),type-name also cannot be atomic or cvr-qualified

      The header<stdatomic.h> definesmany convenience type aliases, fromatomic_bool toatomic_uintmax_t, which simplify the use of this keyword with built-in and library types.

      _Atomicconstint* p1;// p is a pointer to an atomic const intconstatomic_int* p2;// sameconst _Atomic(int)* p3;// same

      If the macro constant__STDC_NO_ATOMICS__ is defined by the compiler, the keyword_Atomic is not provided.

      [edit]Explanation

      Objects of atomic types are the only objects that are free fromdata races; that is, they may be modified by two threads concurrently or modified by one and read by another.

      Each atomic object has its own associatedmodification order, which is a total order of modifications made to that object. If, from some thread's point of view, modificationA of some atomicMhappens-before modificationB of the same atomicM, then in the modification order ofM,A occurs beforeB.

      Note that although each atomic object has its own modification order, there is no single total order; different threads may observe modifications to different atomic objects in different orders.

      There are four coherence kinds that are guaranteed for all atomic operations:

      1. write-write coherence: If an operationA that modifies an atomic objectMhappens-before an operationB that modifiesM, thenA appears earlier thanB in the modification order ofM.
      2. read-read coherence: If a value computationA of an atomic objectM happens before a value computationB ofM, andA takes its value from a side effectX onM, then the value computed byB is either the value stored byX or is the value stored by a side effectY onM, whereY appears later thanX in the modification order ofM.
      3. read-write coherence: If a value computationA of an atomic objectMhappens-before an operationB onM, thenA takes its value from a side effectX onM, whereX appears beforeB in the modification order ofM.
      4. write-read coherence: If a side effectX on an atomic objectMhappens-before a value computationB ofM, then the evaluationB takes its value fromX or from a side effectY that appears afterX in the modification order ofM.

      Some atomic operations are also synchronization operations; they may have additional release semantics, acquire semantics, or sequentially-consistent semantics. Seememory_order.

      Built-inincrement and decrement operators andcompound assignment are read-modify-write atomic operations with total sequentially consistent ordering (as if usingmemory_order_seq_cst). If less strict synchronization semantics are desired, thestandard library functions may be used instead.

      Atomic properties are only meaningful forlvalue expressions. Lvalue-to-rvalue conversion (which models a memory read from an atomic location to a CPU register) strips atomicity along with other qualifiers.

      This section is incomplete
      Reason: more, review interaction with memory_order and atomic library pages

      [edit]Notes

      Accessing a member of an atomic struct/union is undefined behavior.

      The library typesig_atomic_t does not provide inter-thread synchronization or memory ordering, only atomicity.

      Thevolatile types do not provide inter-thread synchronization, memory ordering, or atomicity.

      Implementations are recommended to ensure that the representation of_Atomic(T) in C is same as that ofstd::atomic<T> in C++ for every possible typeT. The mechanisms used to ensure atomicity and memory ordering should be compatible.

      [edit]Keywords

      _Atomic

      [edit]Example

      Run this code
      #include <stdatomic.h>#include <stdio.h>#include <threads.h> atomic_int acnt;int cnt; int f(void* thr_data){for(int n=0; n<1000;++n){++cnt;++acnt;// for this example, relaxed memory order is sufficient, e.g.// atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed);}return0;} int main(void){thrd_t thr[10];for(int n=0; n<10;++n)thrd_create(&thr[n], f,NULL);for(int n=0; n<10;++n)thrd_join(thr[n],NULL); printf("The atomic counter is %u\n", acnt);printf("The non-atomic counter is %u\n", cnt);}

      Possible output:

      The atomic counter is 10000The non-atomic counter is 8644

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.7.2.4 Atomic type specifiers (p: TBD)
      • 7.17 Atomics <stdatomic.h> (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.7.2.4 Atomic type specifiers (p: 87)
      • 7.17 Atomics <stdatomic.h> (p: 200-209)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.7.2.4 Atomic type specifiers (p: 121)
      • 7.17 Atomics <stdatomic.h> (p: 273-286)

      [edit]See also

      Concurrency support library
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/atomic&oldid=180497"

      [8]ページ先頭

      ©2009-2025 Movatter.jp