In computing, amemory model describes the interactions ofthreads throughmemory and their shared use of thedata.
A memory model allows a compiler to perform many important optimizations.Compiler optimizations likeloop fusion move statements in the program, which can influence the order of read and write operations of potentially sharedvariables. Changes in the ordering of reads and writes can causerace conditions. Without a memory model, a compiler may not apply such optimizations to multi-threaded programs at all, or it may apply optimizations that are incompatible with multi-threading, leading to bugs.
Modern programming languages likeJava therefore implement a memory model. The memory model specifiessynchronization 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 arace condition is defined over the order of operations with respect to these memory barriers.[1]
These semantics then give optimizing compilers a higher degree of freedom when applying optimizations: the compiler needs to make sureonly 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:
TheJava 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 alibrary without placing certain restrictions on the implementation and, in particular, that theC andC++ standards (C99 andC++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 andC11.[8][9] TheRust programming language inherited most of C/C++'s memory model.[10]
The Java Memory Model describes what behaviors are legal in multithreaded code, and how threads may interact through memory. It describes the relationship between variables in a program and the low-level details of storing and retrieving them to and from memory or registers in a real computer system. It does this in a way that can be implemented correctly using a wide variety of hardware and a wide variety of compiler optimizations.
C++ threading libraries are in the awkward situation of specifying (implicitly or explicitly) an extended memory model for C++ in order to specify program execution.We propose integrating a memory model suitable for multithreaded execution into the C++ Standard.
This [link farm] provides information related to the effort to clarify the meaning of multi-threaded C++ programs, and to provide some standard thread-related APIs where those are currently missing.
Rust pretty blatantly just inherits the memory model for atomics from C++20.
Thiscomputer-programming-related article is astub. You can help Wikipedia byadding missing information. |