How does Volatile ensure visibility?

Under the x86 processor, use the tool to obtain the assembly instructions generated by the JIT compiler to see what the CPU will do when writing Volatile.

Java code:

instance = new Singleton();//instance is a volatile variable

Assembly code:

0x01a3de1d: movb $0x0,0x1104800(%esi);

0x01a3de24: lock  addl $0x0,(%esp);

When writing a shared variable modified with a volatile variable, there will be a second line of assembly code . By checking the IA-32 architecture software developer's manual, we can see that the lock prefix instruction will cause two things under multi-core processors.

  • The data of the current processor cache line will be written back to the system memory.
  • This operation of writing back to the memory will invalidate the data cached at that memory address in other CPUs.

In order to improve the processing speed, the processor does not directly communicate with the memory, but first reads the data in the system memory to the internal cache (L1, L2 or other) before performing the operation, but after the operation, it is not known when it will be written to the memory , If you write to the declared Volatile variable, the JVM will send a Lock prefix instruction to the processor to write the data of the cache line where the variable is located back to the system memory. But even if it is written back to the memory, if the cache value of other processors is still old, there will be problems when performing calculation operations. Therefore, in order to ensure that the caches of each processor are consistent, the cache will be consistent. Protocol, each processor checks whether the value of its cache is expired by sniffing the data spread on the bus. When the processor finds that the memory address corresponding to its cache line has been modified, it will change the cache line of the current processor Set to invalid state, when the processor wants to modify the data, it will force the data to be read from the system memory to the processor cache again .

Self-understanding: The instance shared variable in the system memory. When multiple threads access this object concurrently, as shown in the figure, cpu1 accesses instance through threadA, and cpu2 accesses instance through threadB. In order to improve processing speed, the processor does not directly communicate with memory, and It is to first read the data of the system memory to the cache and then perform the operation.If the variable instance is modified by the volatile keyword, cpu1 modifies the instance data in the cache and immediately writes it to the system memory through the system bus, and other processors spread it by sniffing the bus. The data is used to check whether the value of its own cache has expired. When the processor finds that the memory address corresponding to its cache line has been modified, it will set the current processor’s cache line to an invalid state, and when the processor wants to modify the data When, it will force the data to be read from the system memory to the processor cache again.

Guess you like

Origin blog.csdn.net/qianzhitu/article/details/103052040