名前空間
変種
操作

std::memory_order

提供: cppreference.com
< cpp‎ | atomic

 
 
アトミック操作ライブラリ
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
atomic(C++11)
atomic_is_lock_free(C++11)
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
atomic_store
atomic_store_explicit
(C++11)
(C++11)
atomic_load
atomic_load_explicit
(C++11)
(C++11)
atomic_exchange
atomic_exchange_explicit
(C++11)
(C++11)
atomic_compare_exchange_weak
atomic_compare_exchange_weak_explicit
atomic_compare_exchange_strong
atomic_compare_exchange_strong_explicit
(C++11)
(C++11)
(C++11)
(C++11)
atomic_fetch_add
atomic_fetch_add_explicit
(C++11)
(C++11)
atomic_fetch_sub
atomic_fetch_sub_explicit
(C++11)
(C++11)
atomic_fetch_and
atomic_fetch_and_explicit
(C++11)
(C++11)
atomic_fetch_or
atomic_fetch_or_explicit
(C++11)
(C++11)
atomic_fetch_xor
atomic_fetch_xor_explicit
(C++11)
(C++11)
アトミックのフラグ
Original:
Atomic flags
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
atomic_flag(C++11)
atomic_flag_test_and_set
atomic_flag_test_and_set_explicit
(C++11)
(C++11)
atomic_flag_clear
atomic_flag_clear_explicit
(C++11)
(C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
atomic_init(C++11)
ATOMIC_VAR_INIT(C++11)
ATOMIC_FLAG_INIT(C++11)
メモリは発注
Original:
Memory ordering
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
memory_order(C++11)
kill_dependency(C++11)
atomic_thread_fence(C++11)
atomic_signal_fence(C++11)
 
Defined in header <atomic>
enum memory_order {

    memory_order_relaxed,
    memory_order_consume,
    memory_order_acquire,
    memory_order_release,
    memory_order_acq_rel,
    memory_order_seq_cst

};
(C + + 11以来)
std::memory_order非アトミックなメモリアクセスはアトミック操作周りに注文する方法を指定します。これの根拠は、同時にマルチコアシステム上のいくつかの変数に読み取りおよび書き込みする際には、いくつかのスレッドが、あるスレッドがを参照してくださいかもしれないということです、別のスレッドがそれを書いているものとは異なる順序で値が変化します。また、変更の見かけの順序はいくつかのリーダー·スレッド間で異なる場合があります。メモリはアトミック変数へのアクセスはすべてが連続していることを保証することは、いくつかのケースでパフォーマンスを傷つけることがあります。 std::memory_orderコンパイラが強制しなければならない正確な制約を指定することができます.
Original:
std::memory_order specifies how non-atomic memory accesses are to be ordered around an atomic operation. The rationale of this is that when several threads simultaneously read and write to several variables on multi-core systems, one thread might see the values change in different order than another thread has written them. Also, the apparent order of changes may be different across several reader threads. Ensuring that all memory accesses to atomic variables are sequential may hurt performance in some cases. std::memory_order allows to specify the exact constraints that the compiler must enforce.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
これは、追加のパラメータを使用して、ライブラリー内の各アトミックな操作でカスタムメモリの順序を指定することが可能です。デフォルトはstd::memory_order_seq_cstです.
Original:
It's possible to specify custom memory order for each atomic operation in the library via an additional parameter. The default is std::memory_order_seq_cst.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] 定数

Defined in header <atomic>
Original:
Value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Explanation
memory_order_relaxed
'リラックス'は順序:アトミック変数の周りにメモリアクセスの順序変更には制約はありません.
Original:
Relaxed ordering: there are no constraints on reordering of memory accesses around the atomic variable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
memory_order_consume
操作 '消費しません':しない、現在ロードされている値に依存し、このロードの前に並べ替えることができ、現在のスレッドで読み込みます。これは、現在のスレッドに表示されている同じアトミック変数を解放し、他のスレッドでの従属変数への書き込みが保証されます。ほとんどのプラットフォームでは、これが唯一のコンパイラの最適化に影響を及ぼし.
Original:
Consume operation: no reads in the current thread dependent on the value currently loaded can be reordered before this load. This ensures that writes to dependent variables in other threads that release the same atomic variable are visible in the current thread. On most platforms, this affects compiler optimization only.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
memory_order_acquire
操作 '取得しない':しない、現在のスレッドを読み込んで、この負荷の前に並べ替えることができます。これは、すべてが同じアトミック変数を解放し、他のスレッドで書き込みが現在のスレッドに表示されていることを保証.
Original:
Acquire operation: no reads in the current thread can be reordered before this load. This ensures that all writes in other threads that release the same atomic variable are visible in the current thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
memory_order_release
'リリース'操作:現在のスレッドに書いてありませんが、この店の後に並べ替えることができます。これは、現在のスレッドに書いてすべてが同じアトミック変数を取得し、他のスレッドに表示されていることを保証.
Original:
Release operation: no writes in the current thread can be reordered after this store. This ensures that all writes in the current thread are visible in other threads that acquire the same atomic variable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
memory_order_acq_rel
'取得放'操作:現在のスレッドを読み込んでないが同様、現在のスレッドに書いてないように、この負荷の前に並べ替えることができますこのストアの後に並べ替えることができます。操作は、リード·モディファイ·ライト操作です。これは、修正や変更が同じアトミック変数を取得し、他のスレッドに表示される前に、すべてが同じアトミック変数を解放し、別のスレッドで書き込みが表示されていることを保証されてい.
Original:
Acquire-release operation: no reads in the current thread can be reordered before this load as well as no writes in the current thread can be reordered after this store. The operation is read-modify-write operation. It is ensured that all writes in another threads that release the same atomic variable are visible before the modification and the modification is visible in other threads that acquire the same atomic variable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
memory_order_seq_cst
'シーケンシャル順序'。操作は習得リリース操作と同じ意味を持っており、さらに連続的に一貫性のある操作の順序を持​​っている.
Original:
Sequential ordering. The operation has the same semantics as acquire-release operation, and additionally has sequentially-consistent operation ordering.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 発注リラックス

タグ付けされたアトミック操作はstd::memory_order_relaxed以下の特性を示す
Original:
Atomic operations tagged std::memory_order_relaxed exhibit the following properties:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • 他のメモリアクセスの順序付けは、一切保証されていません。これは、アトミック変数を使用して複数のスレッドを同期することは不可能であることを意味します.
    Original:
    No ordering of other memory accesses is ensured whatsoever. This means that it is not possible to synchronize several threads using the atomic variable.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 読み込み、それ自体が順序付けられているアトミック変数に書き込みます。スレッドが同じオブジェクトから同じスレッドによって値、後続の読み取りを読み取ると、以前の値を生成することはできません.
    Original:
    Reads and writes to the atomic variable itself are ordered. Once a thread reads a value, a subsequent read by the same thread from the same object can not yield an earlier value.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
たとえば、xy当初はゼロ
Original:
For example, with x and y initially zero,
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

// Thread 1:
r1 = y.load(memory_order_relaxed);
x.store(r1, memory_order_relaxed);
// Thread 2:
r2 = x.load(memory_order_relaxed);
y.store(42, memory_order_relaxed);

r1 == r2 == 42を生成するために許可されている.
Original:
is allowed to produce r1 == r2 == 42.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 発注リリース - 消費する

アトミックストアがタグ付きの場合std::memory_order_releaseと同じ変数から原子負荷がstd::memory_order_consumeタグ付けされて、操作には、以下の特性を示す
Original:
If an atomic store is tagged std::memory_order_release and an atomic load from the same variable is tagged std::memory_order_consume, the operations exhibit the following properties:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • いいえアトミックストアの後で並べ替えることができるライタスレッドに書いてありません
    Original:
    No writes in the writer thread can be reordered after the atomic store
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 読み取りまたはアトミック負荷から受け取った値に依存して書き込むんは、アトミックロードの前に並べ替えることはできません。アドレスまたは値をアトミック変数の値から計算されることを意味する "に依存"。スレッド間の同期のこの形式は、 "依存関係の順序"として知られています.
    Original:
    No reads or writes dependent on the value received from atomic load can be reordered before the atomic load. "Dependent on" means that the address or value is computed from the value of the atomic variable. This form of synchronization between threads is known as "dependency ordering".
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 同期は、スレッドと''かかる同じアトミック変数を解放するとの間に確立される。他のスレッドが同期化されたスレッドのいずれかまたは両方を比べてメモリアクセスの異なる順序を確認することができます.
    Original:
    The synchronization is established only between the threads releasing and consuming the same atomic variable. Other threads can see different order of memory accesses than either or both of the synchronized threads.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 同期が推移的である。我々は次のような状況を持っているなら、それは、次のとおりです
    Original:
    The synchronization is transitive. That is, if we have the following situation:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • スレッド'は、アトミック変数を解放 A.
    Original:
    Thread A releases atomic variable a.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • スレッド Bアトミック変数を消費 A.
    Original:
    Thread B consumes atomic variable a.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • アトミック変数 Bに依存しているA.
    Original:
    Atomic variable b is dependent on a.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • スレッド Bリリースアトミック変数 B.
    Original:
    Thread B releases atomic variable b.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • スレッド Cアトミック変数を消費または取得 B.
    Original:
    Thread C consumes or acquires atomic variable b.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
その後だけでなく、、A B B Cを同期していますが、、A Cも。つまり、すべてのスレッドによって書き込みのリリース前に発売されました Aを完了することが保証されているスレッドは、一度 Cに店舗を観察 B.
Original:
Then not only A and B or B and C are synchronized, but A and C also. That is, all writes by the thread A that were launched before the release of a are guaranteed to be completed once thread C observes the store to b.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
DEC Alphaの以外のすべての主流のCPU上で、依存関係の順序付けは自動的に行われ、追加のCPUの命令は、特定のコンパイラの最適化が影響を受けるため、この同期モードのために発行されていない(コンパイラをEGに関与しているオブジェクトに対する投機的なロードを実行することは禁止されてい依存関係の連鎖)
Original:
On all mainstream CPUs, other than DEC Alpha, dependency ordering is automatic, no additional CPU instructions are issued for this synchronization mode, only certain compiler optimizations are affected (e.g. the compiler is prohibited from performing speculative loads on the objects that are involved in the dependency chain)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] シーケンスを放します

いくつかの原子がある場合はストアがリリースされた、いくつかの他のスレッドがその原子上の読み出し - 変更 - 書き込み操作を実行し、 "解除シーケンス"が形成される:同じ、最初のスレッドと同期アトミックとに読み出し - 変更 - 書き込みを行うすべてのスレッドお互い彼らはmemory_order_releaseセマンティクスを持っていない場合でも。個々の消費者のスレッド間で不必要な同期をかけることなく、可能性のある複数の消費者の状況.. - これは、単一の生産者を作る
Original:
If some atomic is store-released and several other threads perform read-modify-write operations on that atomic, a "release sequence" is formed: all threads that perform the read-modify-writes to the same atomic synchronize with the first thread and each other even if they have no memory_order_release semantics. This makes single producer - multiple consumers situations possible without imposing unnecessary synchronization between individual consumer threads.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] リリース取得順序

アトミックストアがタグ付きの場合std::memory_order_releaseと同じ変数から原子負荷がstd::memory_order_acquireタグ付けされて、操作には、以下の特性を示す
Original:
If an atomic store is tagged std::memory_order_release and an atomic load from the same variable is tagged std::memory_order_acquire, the operations exhibit the following properties:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • いいえアトミックストアの後で並べ替えることができるライタスレッドに書いてありません
    Original:
    No writes in the writer thread can be reordered after the atomic store
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • リーダースレッドで読み込みなしは、アトミックロードの前に並べ替えることができません.
    Original:
    No reads in the reader thread can be reordered before the atomic load.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 同期は、をリリースすると''は同じアトミック変数を取得するスレッド間で確立されます。他のスレッドが同期化されたスレッドのいずれかまたは両方を比べてメモリアクセスの異なる順序を確認することができます.
    Original:
    The synchronization is established only between the threads releasing and acquiring the same atomic variable. Other threads can see different order of memory accesses than either or both of the synchronized threads.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 同期が推移的である。我々は次のような状況を持っているなら、それは、次のとおりです
    Original:
    The synchronization is transitive. That is, if we have the following situation:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • スレッド'は、アトミック変数を解放 A.
    Original:
    Thread A releases atomic variable a.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • スレッド Bアトミック変数を消費 A.
    Original:
    Thread B consumes atomic variable a.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • スレッド Bリリースアトミック変数 B.
    Original:
    Thread B releases atomic variable b.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • スレッド Cアトミック変数を消費または取得 B.
    Original:
    Thread C consumes or acquires atomic variable b.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
その後だけでなく、、A B B Cを同期していますが、、A Cも。つまり、すべてのスレッドによって書き込みのリリース前に発売されました Aを完了することが保証されているスレッドは、一度 Cに店舗を観察 B.
Original:
Then not only A and B or B and C are synchronized, but A and C also. That is, all writes by the thread A that were launched before the release of a are guaranteed to be completed once thread C observes the store to b.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
強く順序付けられたシステム(x86、SPARC、IBMメインフレーム)で、リリース取得順序は自動的に行われます。追加のCPUの命令は、特定のコンパイラの最適化(例えば、コンパイラがアトミックストアrelase過ぎ非アトミックストアを移動することを禁止またはそれ以前のロード取得アトミックより非アトミックロードを実行されている)が影響を受け、この同期モードのために発行されていません
Original:
On strongly-ordered systems (x86, SPARC, IBM mainframe), release-acquire ordering is automatic. No additional CPU instructions are issued for this synchronization mode, only certain compiler optimizations are affected (e.g. the compiler is prohibited from moving non-atomic stores past the atomic store-relase or perform non-atomic loads earlier than the atomic load-acquire)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 連続して一貫性のある順序

アトミックストアとタグ付けされてstd::memory_order_seq_cstと同じ変数から原子負荷がstd::memory_order_seq_cstタグ付けされている場合は、動作は次のような特性を示す
Original:
If an atomic store and an is tagged std::memory_order_seq_cst and an atomic load from the same variable is tagged std::memory_order_seq_cst, then the operations exhibit the following properties:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • いいえアトミックストアの後で並べ替えることができるライタスレッドに書いてありません
    Original:
    No writes in the writer thread can be reordered after the atomic store
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • リーダースレッドで読み込みなしは、アトミックロードの前に並べ替えることができません.
    Original:
    No reads in the reader thread can be reordered before the atomic load.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 同期はstd::memory_order_seq_cstタグ付けされたすべてのアトミックな操作との間に確立される。このような原子操作を使用して、すべてのスレッドは、メモリアクセスの順序と同じ順序を参照してください。.
    Original:
    The synchronization is established between all atomic operations tagged std::memory_order_seq_cst. All threads using such atomic operation see the same order of memory accesses.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
シーケンシャルな順序は、すべての消費者が同じ順序で発生するすべての生産者の行動を観察しなければならない多くの複数のプロデューサ - 複数のコンシューマ場合にも必要です.
Original:
Sequential ordering is necessary for many multiple producer-multiple consumer situations where all consumers must observe the actions of all producers occurring in the same order.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
合計シーケンシャルな順序は、すべてのマルチコアシステム上でフルメモリフェンスCPUの命令を必要とします。それはすべてのメモリはすべてのスレッドに伝播するために強制的にアクセスするので、これはパフォーマンスのボトルネックになるかもしれ.
Original:
Total sequential ordering requires a full memory fence CPU instruction on all multi-core systems. This may become a performance bottleneck since it forces all memory accesses to propagate to every thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 揮発性との関係

{{{1}}}
Original:
{{{2}}}
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]

[編集] std::memory_order_relaxed

次の例では、アトミック性を必要とするタスクを(グローバルカウンタを更新する)を示していますが、非アトミックなメモリ以降に何も順序の制約は一切関与しませんされていません.
Original:
The following example demonstrates a task (updating a global counter) that requires atomicity, but no ordering constraints since non-atomic memory is not involved.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <vector>
#include <iostream>
#include <thread>
#include <atomic>
 
std::atomic<int> cnt = ATOMIC_VAR_INIT(0);
 
void f()
{
    for(int n = 0; n < 1000; ++n) {
        cnt.fetch_add(1, std::memory_order_relaxed);
    }
}
 
int main()
{
    std::vector<std::thread> v;
    for(int n = 0; n < 10; ++n) {
        v.emplace_back(f);
    }
    for(auto& t : v) {
        t.join();
    }
    std::cout << "Final counter value is " << cnt << '\n';
}

Output:

Final counter value is 10000

[編集] std::memory_order_release and std::memory_order_consume

この例では、依存関係順の同期を示しています:整数データは、データの依存関係による文字列へのポインタとは関係ありません、したがって、その値は、消費者に定義されていません.
Original:
This example demonstrates dependency-ordered synchronization: the integer data is not related to the pointer to string by a data-dependency relationship, thus its value is undefined in the consumer.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <thread>
#include <atomic>
#include <cassert>
#include <string>
 
std::atomic<std::string*> ptr;
int data;
 
void producer()
{
    std::string* p  = new std::string("Hello");
    data = 42;
    ptr.store(p, std::memory_order_release);
}
 
void consumer()
{
    std::string* p2;
    while (!(p2 = ptr.load(std::memory_order_consume)))
        ;
    assert(*p2 == "Hello"); // never fires
    assert(data == 42); // may or may not fire
}
 
int main()
{
    std::thread t1(producer);
    std::thread t2(consumer);
    t1.join(); t2.join();
}


[編集] std::memory_order_release and memory_order_acquire

ミューテックス、同時キュー、および他の生産者 - 消費者の状況は、パブリッシャスレッドで発注リリースを必要とし、消費者スレッドの順序獲得。このパターンは、スレッド間のペアワイズ同期を確立.
Original:
Mutexes, concurrent queues, and other producer-consumer situations require release ordering in the publisher thread and acquire ordering in the consumer thread. This pattern establishes pairwise synchronization between threads.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <thread>
#include <atomic>
#include <cassert>
#include <string>
 
std::atomic<std::string*> ptr;
int data;
 
void producer()
{
    std::string* p  = new std::string("Hello");
    data = 42;
    ptr.store(p, std::memory_order_release);
}
 
void consumer()
{
    std::string* p2;
    while (!(p2 = ptr.load(std::memory_order_acquire)))
        ;
    assert(*p2 == "Hello"); // never fires
    assert(data == 42); // never fires
}
 
int main()
{
    std::thread t1(producer);
    std::thread t2(consumer);
    t1.join(); t2.join();
}


[編集] std::memory_order_acq_rel

フォローの例では、他動詞つのスレッド間の順序付けリリース獲得を示しています
Original:
The follow example demonstrates transitive release-acquire ordering across three threads
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <thread>
#include <atomic>
#include <cassert>
#include <vector>
 
std::vector<int> data;
std::atomic<int> flag = ATOMIC_VAR_INIT(0);
 
void thread_1()
{
    data.push_back(42);
    flag.store(1, std::memory_order_release);
}
 
void thread_2()
{
    int expected=1;
    while (!flag.compare_exchange_strong(expected, 2, std::memory_order_acq_rel)) {
        expected = 1;
    }
}
 
void thread_3()
{
    while (flag.load(std::memory_order_acquire) < 2)
        ;
    assert(data.at(0) == 42); // will never fire
}
 
int main()
{
    std::thread a(thread_1);
    std::thread b(thread_2);
    std::thread c(thread_3);
    a.join(); b.join(); c.join();
}


[編集] std::memory_order_seq_cst

この例では、シーケンシャルな順序付けが必要である状況を示しています。スレッドに対してcdxy逆の順序でアトミックに変化を観察することが可能になるため、その他の順序は、assertの引き金となるかもしれません.
Original:
This example demonstrates a situation where sequential ordering is necessary. Any other ordering may trigger the assert because it would be possible for the threads c and d to observe changes to the atomics x and y in opposite order.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <thread>
#include <atomic>
#include <cassert>
 
std::atomic<bool> x = ATOMIC_VAR_INIT(false);
std::atomic<bool> y = ATOMIC_VAR_INIT(false);
std::atomic<int> z = ATOMIC_VAR_INIT(0);
 
void write_x()
{
    x.store(true, std::memory_order_seq_cst);
}
 
void write_y()
{
    y.store(true, std::memory_order_seq_cst);
}
 
void read_x_then_y()
{
    while (!x.load(std::memory_order_seq_cst))
        ;
    if (y.load(std::memory_order_seq_cst)) {
        ++z;
    }
}
 
void read_y_then_x()
{
    while (!y.load(std::memory_order_seq_cst))
        ;
    if (x.load(std::memory_order_seq_cst)) {
        ++z;
    }
}
 
int main()
{
    std::thread a(write_x);
    std::thread b(write_y);
    std::thread c(read_x_then_y);
    std::thread d(read_y_then_x);
    a.join(); b.join(); c.join(); d.join();
    assert(z.load() != 0);  // will never happen
}