Search⌘ K

The Three Fences

Explore the three types of memory fences in modern C++ concurrency—full, acquire, and release fences. Understand how these fences manage operation ordering and synchronization to write correct lock-free programs. Visual examples clarify the subtle interactions between loads and stores around these memory barriers.

We'll cover the following...

Typically, three kinds of fences are used: full fence, acquire fence and release fence. As a reminder, acquire is a load, and release is a store operation. What happens if I place one of the three memory barriers between the four combinations of load and store operations?

  • Full fence: A full fence std::atomic_thread_fence() between two arbitrary operations prevents the reordering of these operations, but guarantees that it won’t hold for StoreLoad operations. Also, they can be reordered.

  • Acquire fence: An acquire fence std::atomic_thread_fence(std::memory_order_acquire) prevents a ...