atomic_thread_fence
| Defined in header <stdatomic.h>
|
||
void atomic_thread_fence( memory_order order );
|
(since C11) | |
Establishes memory synchronization ordering of non-atomic and relaxed atomic accesses, as instructed by order, without an associated atomic operation. Note however, that at least one atomic operation is required to set up the synchronization, as described below.
Fence-atomic synchronization
A release fence F in thread A synchronizes-with atomic acquire operation Y in thread B, if
- there exists an atomic store X (with any memory order),
- Y reads the value written by X (or the value would be written by release sequence headed by X if X were a release operation),
- F is sequenced-before X in thread A.
In this case, all non-atomic and relaxed atomic stores that are sequenced-before F in thread A will happen-before all non-atomic and relaxed atomic loads from the same locations made in thread B after Y.
Atomic-fence synchronization
An atomic release operation X in thread A synchronizes-with an acquire fence F in thread B, if
- there exists an atomic read Y (with any memory order),
- Y reads the value written by X (or by the release sequence headed by X),
- Y is sequenced-before F in thread B.
In this case, all non-atomic and relaxed atomic stores that are sequenced-before X in thread A will happen-before all non-atomic and relaxed atomic loads from the same locations made in thread B after F.
Fence-fence synchronization
A release fence FA in thread A synchronizes-with an acquire fence FB in thread B, if
- there exists an atomic object M,
- there exists an atomic write X (with any memory order) that modifies M in thread A,
- FA is sequenced-before X in thread A,
- there exists an atomic read Y (with any memory order) in thread B,
- Y reads the value written by X (or the value would be written by release sequence headed by X if X were a release operation),
- Y is sequenced-before FB in thread B.
In this case, all non-atomic and relaxed atomic stores that are sequenced-before FA in thread A will happen-before all non-atomic and relaxed atomic loads from the same locations made in thread B after FB.
Depending on the order, this operation
- has no effects, if
order == memory_order_relaxed - is an acquire fence, if
order == memory_order_acquire || order == memory_order_consume - is a release fence, if
order == memory_order_release - is both an acquire fence and a release fence, if
order == memory_order_acq_rel - is a sequentially consistent acquire and release fence, if
order == memory_order_seq_cst
Parameters
| order | - | the memory ordering executed by this fence |
Return value
(none)
Notes
On x86 (including x86-64), atomic_thread_fence functions issue no CPU instructions and only affect compile-time code motion, except for atomic_thread_fence(memory_order_seq_cst).
atomic_thread_fence imposes stronger synchronization constraints than an atomic store operation with the same memory_order. While an atomic store-release operation prevents all preceding reads and writes from moving past the store-release, an atomic_thread_fence with memory_order_release ordering prevents all preceding reads and writes from moving past all subsequent stores.
References
- C17 standard (ISO/IEC 9899:2018):
- 7.17.4.1 The atomic_thread_fence function (p: 204)
- C11 standard (ISO/IEC 9899:2011):
- 7.17.4.1 The atomic_thread_fence function (p: 278-279)
See also
(C11) |
fence between a thread and a signal handler executed in the same thread (function) |
C++ documentation for atomic_thread_fence
| |