Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
次の 2 つのバリアントを検討してください。
std::atomic<int> a; a = 1; int b = a;
と
std::atomic<int> a; a.store(1); int b = a.load();
ドキュメントから、2番目のものは完全にアトミックであることがわかりますが、いつどれを使用する必要があり、どのような違いが詳細にあるのかわかりません。
これら 2 つの例は同等です。operator=andは、引数のデフォルト値を使用して、それぞれandoperator Tを呼び出すことと同等であると定義されています。storeloadmemory_order
operator=
operator T
store
load
memory_order
各アクセスがメモリ フェンスとして機能するように、そのデフォルト値 に満足している場合はmemory_order_seq_cst、より適切に見える方を使用してください。別の値を指定する場合は、関数を使用する必要があります。これは、演算子が 2 番目の引数を受け入れることができないためです。
memory_order_seq_cst