Home > Mobile >  About std::memory_order, Am I understood right?
About std::memory_order, Am I understood right?

Time:10-01

on c reference.com says memory_order::seq_cst ::

A load operation with this memory order performs an acquire operation, a store performs a release operation, and read-modify-write performs both an acquire operation and a release operation, plus a single total order exists in which all threads observe all modifications in the same order.

[ Q1 ] :: this mean the order go straight down through "every operations of all (others this) atomic_vars with memory_order::seq_cst" ?

[ Q2 ] :: And release , acquire and rel_acq are not included in "single total order" ?

Can you please answer me about Q1 and Q2. I understood that seq_cst is same as other three with write, read and write_read operation, but confusing about does seq_cst can order other atomic_vars too, not only same var.

CodePudding user response:

cppreference is only a summary of the C standard, and sometimes its text is less precise. The actual standard draft makes it clear: C 20 N4680 atomics.order p4:

There is a single total order S on all memory_order::seq_cst operations, including fences, that satisfies the following constraints [...]

This clearly says all seq_cst operations, not just all operations on a particular object.

And notes 6 and 7 further down emphasize that the order does not apply to weaker memory orders:

6 [Note: We do not require that S be consistent with “happens before” (6.9.2.1). This allows more efficient implementation of memory_order::acquire and memory_order::release on some machine architectures. It can produce surprising results when these are mixed with memory_order::seq_cst accesses. — end note]

7 [Note: memory_order::seq_cst ensures sequential consistency only for a program that is free of data races and uses exclusively memory_order::seq_cst atomic operations. Any use of weaker ordering will invalidate this guarantee unless extreme care is used. In many cases, memory_order::seq_cst atomic operation

  • Related