Memory Model in Concurrent Programming Explained
- Operations per thread
- count
- Result
- Threads
- 4 count
- You entered
- Reads per thread
- 1000 count
- You entered
This article explains what a memory model is in concurrent programming: the set of rules that defines how threads interact through shared memory, what values a read can return, and what guarantees you can rely on when writing multithreaded code. You will learn the core concepts, why they matter, common pitfalls, and how to choose a model for your language or system.
What Is a Memory Model?
A memory model is a formal specification that answers one question: under what conditions is a read of a shared variable guaranteed to see a particular write? It defines the allowed orderings of memory operations (reads, writes, and synchronization actions) across threads. Without such rules, compilers and CPUs are free to reorder instructions, cache values in registers, or use per-core caches, which can produce results that violate intuitive sequential reasoning.
The term comes from the hardware world: early multiprocessors had weak consistency guarantees, and language designers needed a way to describe what programmers could assume. The Java Memory Model (JMM) and C++11 memory model are the best-known formal examples. They are not optional—they define the semantics of every shared variable access in the language.
Why Do We Need a Formal Model?
Without a memory model, a multithreaded program is undefined behavior. Consider two threads: one writes x = 1 then sets flag = true; the other spins until flag is true, then reads x. Intuitively, the second thread should see x == 1. But on real hardware, the write to x might be delayed in a store buffer, or the compiler might reorder the writes. The memory model specifies whether that intuition holds—and in this case, it does only if the flag write is a release operation and the read is an acquire operation.
Formal models also enable compiler optimizations. If a language had no memory model, the compiler could not reorder or eliminate loads/stores without risking breaking programs. The model defines a boundary: optimizations are allowed as long as they do not violate the guarantees. This balance is why modern languages have precise, often complex, memory models.
Key Concepts: Happens-Before and Synchronization
The central idea in most memory models is the happens-before relation. An action a happens-before action b if:
- a and b are in the same thread and a precedes b in program order.
- a is a write to a variable and b is a read of the same variable that sees that write (in a data-race-free program).
- a is a synchronization action (e.g., unlocking a mutex, writing to a volatile variable) and b is the corresponding acquire (e.g., locking the same mutex, reading the volatile).
- The relation is transitive: if a happens-before b and b happens-before c, then a happens-before c.
If a read sees a write, and the write happens-before the read, then the read is guaranteed to see that write. If there is no happens-before edge, the read may see any value, including stale or completely unexpected ones. This is why data races (two threads accessing the same variable without synchronization, at least one writing) are undefined behavior in C++ and Java—the model gives no guarantees for them.
Sequential Consistency vs. Relaxed Models
Sequential consistency (SC) is the strongest and most intuitive model: all operations appear to execute in some global order, and each thread’s operations appear in program order. SC is what most programmers assume, but it is expensive to implement on modern hardware because it requires global ordering of all memory operations.
Relaxed models (also called weak or weak-ordered) allow more reordering. The C++11 model provides several memory orders:
memory_order_relaxed: no ordering guarantees except atomicity; only the modification order of the specific variable is consistent.memory_order_acquire/memory_order_release: pairs of operations that create happens-before edges. A release write synchronizes with an acquire read that reads its value.memory_order_acq_rel: both acquire and release semantics (used for read-modify-write operations).memory_order_seq_cst: sequentially consistent, the default for C++ atomics and Java volatile.
Java’s model is simpler: volatile gives sequential consistency for the volatile variable itself, and synchronized blocks create happens-before edges. However, Java’s model has known subtleties, such as the “broken” semantics of volatile long/double in older versions (now fixed).
Data Races and Undefined Behavior
A data race occurs when two threads access the same memory location, at least one access is a write, and there is no synchronization (no happens-before edge) between them. In C++ and Rust, a data race is undefined behavior, meaning the compiler may do anything—including producing code that crashes or loops infinitely. In Java, a data race does not cause undefined behavior but the read may return a stale or arbitrary value.
This is a critical distinction: even if a data race appears to work in testing, it is not reliable. The compiler may reorder operations differently at different optimization levels, or the CPU may behave differently on other hardware. The only safe approach is to eliminate data races entirely using locks, atomics, or higher-level constructs like message passing.
How to Choose a Memory Model Strategy
In practice, you rarely choose a memory model; you choose a synchronization mechanism that uses one. The decision tree is:
- If you need a simple flag or counter, use an atomic with
seq_cst(the default) unless profiling shows it is a bottleneck. - If you are publishing data (e.g., a pointer to an immutable object), use a release write and an acquire read—this is the classic “publish” pattern.
- If you only need atomicity without ordering (e.g., a counter that can be slightly stale),
relaxedis acceptable. - If you need a complex invariant, use a mutex—do not try to hand-roll lock-free code with relaxed atomics unless you are an expert.
For most application code, the best choice is to avoid low-level atomics altogether and use higher-level abstractions: threads plus locks, channels, or immutable data. The memory model only matters when you drop to atomics or volatile.
Common Misconceptions and Pitfalls
One common misconception is that volatile in C++ gives atomicity. It does not—it only prevents the compiler from caching the value in a register. Volatile in C++ is for memory-mapped I/O, not for multithreading. In Java, volatile does give atomicity for reads/writes of the variable itself, but not for compound operations like x++.
Another pitfall is assuming that a data race will simply produce a “wrong” value. In reality, it can break the entire program—for example, a compiler might hoist a load out of a loop, causing an infinite loop. The C++ standard explicitly says that a data race is undefined behavior, so there are no limits to what can go wrong.
Finally, many programmers believe that sequentially consistent atomics are always as fast as relaxed ones. On x86, the hardware is already fairly strong, so the difference is small. On ARM and POWER, the difference can be significant. But correctness is far more important than micro-optimization; only switch to relaxed orders after profiling and careful reasoning.
Memory Models in Different Languages
- C++11: Provides a full memory model with six memory orders, atomics, and the happens-before relation. Data races are undefined behavior.
- Java: The JMM defines happens-before for synchronized blocks, volatile, and final fields. Data races are not undefined behavior, but reads may see stale values.
- Rust: Inherits C++’s memory model but enforces data-race freedom at compile time through ownership and Send/Sync traits. Atomics have the same memory orders as C++.
- C#: Uses a model similar to Java, with volatile and locks, but the exact guarantees are less formally specified than C++ or Java.
- Go: The memory model is defined by the language specification, with synchronization via goroutines, channels, and the sync package. It is simpler than C++ but still requires care.
Each language’s model is designed to match its intended use. C++ and Rust target systems programming with zero-cost abstractions, so they expose low-level controls. Java and C# target application development, so they provide simpler, more restrictive models.
Frequently Asked Questions
What is the difference between a memory model and a cache coherence protocol?
A cache coherence protocol (like MESI) ensures that all cores see a consistent value for a single memory location—it prevents two cores from having different values for the same address. A memory model is a higher-level contract about the order of operations across multiple locations. Coherence is about what value a read returns; the memory model is about when that value becomes visible relative to other operations.
Can I write correct multithreaded code without knowing the memory model?
Yes, if you use high-level synchronization primitives like mutexes, channels, or thread-safe queues. These primitives internally use the correct memory barriers. You only need to understand the memory model if you are writing lock-free code or implementing your own synchronization primitives.
Is sequential consistency always the best choice?
No. Sequential consistency is the easiest to reason about but can be slower on weakly-ordered hardware because it forces the compiler and CPU to insert memory barriers. For most code, the performance difference is negligible, but in tight loops or high-performance libraries, relaxed orders can help.
What happens if I use a data race in C++?
It is undefined behavior. The compiler may assume it never happens and optimize accordingly, which can lead to crashes, infinite loops, or corrupted data. Even if it seems to work, it is not guaranteed to work on another platform or with different optimization flags.
Does the memory model apply to single-threaded programs?
No. In a single-threaded program, there is no concurrency, so the memory model is irrelevant. However, the compiler still follows the “as-if” rule, which allows any optimization that does not change the observable behavior of the program.
What is the “happens-before” relation in simple terms?
It is a formal way of saying that one action is guaranteed to be visible to another action. If action A happens-before action B, then A’s effects are visible to B. The relation is established by program order, synchronization operations, and transitivity.
Related
Formula used
in t | Threads | count | 4 | 1 in r | Reads per thread | count | 1000 | 1 out total | Total operations | count | t * r | 0 out per_thread | Operations per thread | count | r | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.