Fences
C++ support two kind of fences: a std::atomic_thread_fence
and a std::atomic_signal_fence
.
-
std::atomic_thread_fence
: synchronises memory accesses between threads. -
std::atomic_signal_fence
: synchronises between a signal handler and code running on the same thread.
std::atomic_thread_fence
A std::atomic_thread_fence
prevents specific operations from crossing a fence.
std::atomic_thread_fence
needs no atomic variable. They are frequently just referred to as fences or memory barriers. You quickly get an idea of what a std::atomic_thread_fence
is all about.
Fences as Memory Barriers
What does that mean? Specific operations cannot cross a memory barrier. What kind of operations? From a bird’s-eye view, we have two kinds of operations: read and write or load and store operations. The expression if(resultRead) return result
is a load, followed by a store operation.
There are four different ways to combine load and store operations:
- LoadLoad: A load followed...