Memory Consistency Errors

Memory consistency errors occur when different threads have inconsistent views of what should be the same data.
What is a Memory Consistency Errors?

one of Thread most 3 errors

different threads have inconsistent views of what should be the same data

image.png

The causes of memory consistency errors are complex, Fortunately, we don’t have to need a detailed understanding of these causes.

we need a strategy for avoiding them.

The key to avoid MC errors is understanding Happen-before relationship

Happen-before

a relationship guarantee memory writes by one specific statement are visible to another specific statement

for our example, Change in Thread A will visible to Thread B

image.png

the value will lost, because there’s no guarantee that thread A’s change to i will be visible to thread B

To create a happen-before relation, we can use synchronization

We already see happen-before relationship

  • Thread.start() when a statement invoke start(), every statement happen-before the statement also has same relation with new thread’s statement

    means that the effects of creating new thread are visible

  • Thread.join all the statements executed by the terminated thread have a happens-before relationship with all the statements following the successful join

    the effect of terminated thread now visiable to the thread performed join

Last modified March 8, 2025: interview (3799c36)