liveBook · Manning

Memory barrier

As explained in C# Concurrency: Asynchronous and multithreaded programming

A memory barrier is a synchronization operation that prevents reads and writes from being reordered across it.

A memory barrier is a synchronization operation that prevents the compiler and CPU from moving reads or writes across it in either direction. It belongs to the memory model, which defines permitted memory-access reordering. Acquire operations prevent reads from moving earlier; release operations prevent writes from moving later. section 7.2

Use the ordering guarantees to reason about shared state:

  • Acquiring a lock provides acquire semantics and exposes up-to-date memory values. section 7.2
  • Releasing a lock provides release semantics and makes changes available to the next thread entering the lock. section 7.2
  • For simple multithreading, use locks whenever accessing data shared between threads; lock misuse can still lead to deadlocks. section 7.2
Figure 7.1 Acquire and release semantics

Use the acquire/release diagram to visualize which reads and writes may cross synchronization boundaries. section 7.2

How C# Concurrency: Asynchronous and multithreaded programming covers it

How deep it goes
Defines acquire, release, and barrier semantics, then stops short of detailed CPU-specific implementation.
Examples
Uses a shared counter and initialization assignment to illustrate compiler and memory-access reordering.
What it assumes
Assumes basic compiler, CPU, memory, and multithreading concepts while defining the relevant ordering semantics.
What you can do after
Evaluate shared-data synchronization and choose locking as the straightforward ordering safeguard.
How it connects
Connects memory barriers with acquire semantics, release semantics, the memory model, locks, and deadlocks.

Also explained in

Compare how 3 books explain memory barrier →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C# Concurrency: Asynchronous and multithreaded programming. The section links point to the parts of the book it draws on.