Reorder and memory barrier directives

Source: Internet
Author: User

References and related literatures:

    • [Java Concurrency Programming art] (https://book.douban.com/subject/26591326/)
    • JMM Cookbook
    • Synchronized-doug Lee
    • JSR 133

Students with good English and time are advised to take a glance at this blog post to read the reference content.

1. Write buffer

Now the processor uses the write buffer to temporarily save the data written to memory, the advantage of the write buffer is:

    1. it can ensure the continuous operation of the instruction, avoid the delay due to the processor waiting to write to memory ;
    2. Flushing the write buffer in batches , and merging multiple writes of the same memory address in the write buffer, can reduce the memory bus footprint .

because the write buffer is visible only to its own processor, the sequence of memory operations for each processor may be inconsistent with the order in which the actual memory operation (that is, the order in which the data was modified and changed) is performed. . The schematic is as follows:

Modern processors use buffers, and both write-and-read Store-Load operations are allowed to be reordered:

2. Memory barrier directives for the Java memory model

To prohibit a processor-specific type of reordering (usually referred to as the x86 platform Store-Load ), the Java compiler prohibits certain types of handlers from being reordered by inserting memory barrier directives where appropriate (with code semantics always). The four types of memory barrier directives are as follows:

1. Load-Load Barrier:    示例:Load1;LoadLoad;Load2    解释:保证装载动作Load1早于Load2即以后所有的Loadn动作,但是对于屏障前后的Store操作并无影响;2. Store-Store Barrier:    示例:Stroe1;StroeStore;Store2    解释:确保Stroe1动作将数据刷新到内存(使得数据对其他处理器可见),早于Store2及其以后所有Storen动作的执行。同理对Loadn操作无影响;3. Load-Stroe Barrier:    示例:Load1;Load-Stroe;Store2:    解释:屏障指令之前的所有Load操作都早于屏障之后所有指令的装载动作(刷新数据到主存);4. Store-Load barriers——全能型,    1)保证屏障之前的所有访问操作完成之后才执行屏障之后的内存访问操作——所谓访问操作包括读取和装载。

[Note]: Note Store-Load is all-in-one, and will mask the rearrangement of all types of instructions before and after .

3. Implementation of volatile memory semantics

Java memory model to achieve volatile visibility and prohibit instructions reflow Two semantics, insert the policy using the following memory barrier:

    1. volatileInsert Store-Store a barrier in front of each write operation and insert a barrier behind it Store-Load(全能) ;

    2. volatileInsert Load-Load barriers and barriers in front of each reading operation Load-Stroe ;


: The write volatile barrier directive insertion strategy ensures that volatile all writes are flushed to main memory before writing and visible to all processors. The all-purpose barrier instruction is then re-ordered in order to avoid writing volatile and subsequent read and write volatile instructions.

volatilewhen read, two instructions are inserted thereafter to prevent volatile the read operation from reordering subsequent reads and writes.

4. Memory barrier in simple interest mode
public class Singleton {    private static volatile Singleton singleton=null;    private Singleton(){}    public static Singleton getSingleton(){        if(singleton==null)        //此处读取singleton,后边加入load-load和load-store屏障        {            synchronized (Singleton.class){                if(singleton==null)                //此处读取singleton,后边加入load-load和load-store屏障                {                    //此处写singleton:加入Store-Store屏障                    singleton=new Singleton();                    //fixme 此处写singleton:加入Store-Load屏障,                    // fixme 此处屏障防止了第二个线程对主存数据singleton读取早于此前线程创建singleton并装载进主存之前                }            }        }        return singleton;    }}

Reorder and memory barrier directives

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.