Memory model (programming)
![]() | It has been suggested that Memory ordering be merged into this article. (Discuss) Proposed since February 2010. |
In computing, a Memory model describes how threads interact through memory, or more generally specify what assumptions the compiler is allowed to make when generating code for segmented memory or paged memory platforms.
History and significance
The significance of a memory model is most easily explained by referring to languages that have no memory model. If a language has no memory model then this prevents a compiler from performing many important optimizations. Even simple compiler optimizations like Loop fusion move statements in the program, and therefore influence the order of read and write operations of potentially shared variables. If the ordering of reads and writes changes, this can cause Race conditions. Without a memory model, a compiler is therefore not allowed to apply such optimizations to multi-threaded programs in general, or only in special cases.
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 entirely defined over the order of operations with respect to these memory barriers.
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:
- 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.
- 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[1]. Since then, the need for a memory model has been more widely accepted, and efforts are underway to provide such semantics for languages like C++0x, the next version of C++[2][3].
See also
- Memory ordering
- Memory barrier
- C memory model
- Consistency model
- Flat memory model
- Java Memory Model
- Segmented memory
References
- ^ Goetz, Brian (2004-02-24). "Fixing the Java Memory Model, Part 1". Retrieved 2008-02-17.
- ^ Boehm, Hans (2005-03-04). "Memory Model for Multthreaded C++". Retrieved 2008-02-17.
- ^ Boehm, Hans. "Threads and memory model for C++". Retrieved 2008-02-17.
External links
This article has not been added to any content categories. Please help out by adding categories to it so that it can be listed with similar articles, in addition to a stub category. (September 2010) |