Skip to content

Memory model (programming)

对于支持multiple thread的programming language,designer可能需要为其设计memory model,典型的案例就是C++、C、Java。

Wikipedia Memory model (programming)

This article is about a concept in multi-thread programming. For details of memory addressing, see Memory address § Memory models.

In computing, a memory model describes the interactions of threads through memory and their shared use of the data.

History and significance

A memory model allows a compiler to perform many important optimizations. Even simple compiler optimizations like loop fusion move statements in the program, which can influence the order of read and write operations of potentially shared variables. Changes in the ordering of reads and writes can cause race conditions. Without a memory model, a compiler is not allowed to apply such optimizations to multi-threaded programs in general, or only in special cases.

NOTE: 上面所描述的内容属于Compile-time memory ordering,在Wikipedia Memory orderingCompile-time memory ordering 章节中进行了具体描述。

Modern programming languages like Java therefore implement a memory model. The memory model specifies synchronization barriers that are established via special, well-defined synchronization operations such as acquiring a lock by entering a synchronized block or method. The memory model stipulates(规定) that changes to the values of shared variables only need to be made visible to other threads when such a synchronization barrier is reached. Moreover, the entire notion of a race condition is defined over the order of operations with respect to these memory barriers.[1]

NOTE: 没有读懂

These semantics then give optimizing compilers a higher degree of freedom when applying optimizations: the compiler needs to make sure only that the values of (potentially shared) variables at synchronization barriers are guaranteed to be the same in both the optimized and unoptimized code. In particular, reordering statements in a block of code that contains no synchronization barrier is assumed to be safe by the compiler.

Most research in the area of memory models revolves around:

1、Designing a memory model that allows a maximal degree of freedom for compiler optimizations while still giving sufficient guarantees about race-free and (perhaps more importantly) race-containing programs.

2、Proving program optimizations that are correct with respect to such a memory model.

The Java Memory Model was the first attempt to provide a comprehensive threading memory model for a popular programming language.[2] After it was established that threads could not be implemented safely as a library without placing certain restrictions on the implementation and, in particular, that the C and C++ standards (C99 and C++03) lacked necessary restrictions,[3][4] the C++ threading subcommittee(小组委员会) set to work on suitable memory model; in 2005, they submitted C working document n1131[5] to get the C Committee on board with their efforts. The final revision of the proposed memory model, C++ n2429,[6] was accepted into the C++ draft standard at the October 2007 meeting in Kona.[7] The memory model was then included in the next C++ and C standards, C++11 and C11.[8][9]

Programming language memory model and CPU memory model

Programming language memory model 需要能够容纳下当今主流CPU memory model,案例包括:

1、C++ memory model,参见 C++\Guide\Memory-model-and-atomic-library\Design