What are read barriers and write barriers in a synchronized block

I study how a synchronized and mutable variable works in java, and I came across a concept called a read and write barrier. Can someone help me understand the meaning of these terms

+7
java multithreading
source share
4 answers

(the answers above are quite complete), I just want to demonstrate a concept with a simple outline

Thread 1 Thread 2 | | | | | | | Everything Thread 1 | | wrote before here | | | | | _ _ _ _ _ _ _ _ _ _ | ( write barrier) (happens before) (read barrier) | | _ _ _ _ _ _ _ _ | | is guaranteed | | to be visible to | | Thread 2 | | | 
+5
source share

A memory barrier is a conceptual “line” in your code that prevents the compiler from making certain optimizations and can insert special “synchronizing” commands into the processor. Typically, the compiler can look within a particular method and see that some instructions can be moved without changing the code value. For example, if you have

 int x = 0, y = 0; x++; y++; 

If the compiler realized that there was any benefit, it might instead output code for

 y++; x++; 

However, if x and y are fields in some class so that they can be seen from other threads, another thread may change values ​​while your method is running.

The memory limit forces the compiler to double-check the values ​​of specific variables (in Java, those that are volatile and Atomic* ), if some other thread changes them when the method starts and it does not allow the compiler to reorder, which can accidentally change the calculation results. On systems supporting multiple cores / processors, the compiler will also force the processor to check that another processor or hardware device does not change the variable at the same time. Java (on Java 5) has an extremely well-defined set of rules for how this works before it happens .

There are some helpful explanations in this FAQ that were written at the time the Java memory model was being developed. Please note that although the concept of a memory barrier is cross-linguistic, most languages ​​do not have as clearly defined rules about them as Java.

+2
source share

When you enter a synchronized block of code, you pass a “read barrier”, and when it exits, you pass a “write barrier”.

Is is used with respect to volatile attributes and provides guidance when Threads should update their volatile attribute values. They should update it when they pass the read barrier, if anyone else has passed the write barrier.

A similar reading from the volatile attribute makes the yout stream a skip read barrier and writing to a mutable attribute makes you skip the write barrier, which is therefore much finer than a synchronized block.

0
source share

A read and write preface is used to implement the semantics of the Java memory model at the lowest level using the JVM.

However, this terminology is missing from the Java Language Specification, which only explains what happens before the relationship. In particular,

  • writing to the volatile variable occurs - before the subsequent reading of the same variable
  • exit from the synchronized block occurs - before the subsequent input of the same synchronized block

When there is a relationship between events between two actions in your program, you have a guarantee that these two actions will be executed in a sequentially consistent order (that is, if there was only one thread and without non-intuitive reordering).

Nesting JVM implementation details is not required to write the correct multithreaded program. However, if you need gory details, the interesting JSR-133 cookbook read.

0
source share

All Articles