cpp/atomic/atomic thread fence

Establishes memory synchronization ordering of non-atomic and relaxed atomic accesses, as instructed by, 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

Return value
(none)

Example
Scan an array of mailboxes, and process only the ones intended for us, without unnecessary synchronization. This example uses atomic-fence synchronization.