14.2.4 What Can You Trust?

You most definitely cannot trust your intuition.

What can you trust?

It turns out that there are a few reasonably simple rules that allow you to make good use of memory barriers. This section derives those rules, for those who wish to get to the bottom of the memory-barrier story, at least from the viewpoint of portable code. If you just want to be told what the rules are rather than suffering through the actual derivation, please feel free to skip to Section [*].

The exact semantics of memory barriers vary wildly from one CPU to another, so portable code must rely only on the least-common-denominator semantics of memory barriers.

Fortunately, all CPUs impose the following rules:

  1. All accesses by a given CPU will appear to that CPU to have occurred in program order.
  2. All CPUs' accesses to a single variable will be consistent with some global ordering of stores to that variable.
  3. Memory barriers will operate in a pair-wise fashion.
  4. Operations will be provided from which exclusive locking primitives may be constructed.

Therefore, if you need to use memory barriers in portable code, you can rely on all of these properties.14.1Each of these properties is described in the following sections.



Subsections
Paul E. McKenney 2011-12-16