Data race free is a very important concept of multi-threaded programs, because the memory models of Java and C ++ are both based on data race free. This article will introduce the origin of this concept, another article, understanding of data race free, introduces its main ideas.
Things will be traced back to the distant 1979 s. Lamport put forward the concept of widespread use in the memory model field in his famous paper "How To Make A Multiprocessor computer that correctly executes Multiprocess programs: sequential consistency, that is, sequence consistency. This article tells us what conditions need to be met for a multi-processor computer to ensure the correctness of the program. Of course, the program runs on different processors and shares the same memory. Although we don't talk about multi-processor now, we all talk about multi-core and multi-threading, but the nature of the problem remains unchanged. It means that multiple execution units complete a task together and communicate with each other through shared storage units. In this case, what kind of support does the underlying system need, in order to ensure that the computing result is the same as the programmer's expectation.
In 1986 seven years later, Dubois, scheurich, and Briggs expanded Lamport's work in the memory access Buffering in multiprocessors paper. They proposed a framework, it is used to analyze consistency issues in a multi-processor system with shared memory. Three States are introduced in this paper. Based on this, the concept of strong ordering is proposed, and the sequential consistency proposed by Lamport is consistent. However, strong ordering imposes too many restrictions on memory operations and hinders system performance. Therefore, they proposed the concept of weak ordering to improve system performance. The system that satisfies weak ordering is not sequential consistency. Programmers need to declare the synchronization variables themselves to ensure the correctness of the program.
After another four years, in 1990, the adve daema took the lead. The daema is now the authoritative in the field of memory models. Java and C ++ both have credit for the establishment of memory models, the very important concept of data race free in the Java and C ++ memory models was proposed by the adve aunt over the past year.
In this article weak ordering-a new definition, adve defines weak ordering proposed by Dubois and makes some modifications to further improve the system performance. The new definition is out of the idea that programmers are used to using sequential consistency to deduce the running results of programs, and the underlying system wants to achieve higher performance, the sequential consistency memory model cannot be used to run programs. So
How can programmers use sequential consistency to infer program results, and the underlying implementation can carry out various optimizations?
Solution: Synchronize the program itself.
This memory model ensures that if your program is synchronized enough, it runs on my weak oerding memory model, I can ensure that the results are the same as those you run in the sequential consistency model.
In this way, the programmer can use sequential consistency to deduce the program results to ensure correct synchronization of the program, and the underlying layer can be flexibly optimized to improve system performance.
Here is a key question:
What is "enough synchronization"
Adve puts forward the concept of data race free. That is to say, if your program meets the data race free conditions, your synchronization will be enough. "Enough" means that, this program runs on weak ordering and on sequential consistency, and the result is the same ~
The new definition provided by adve for weak ordering is:
Hardware is weakly ordered with respect to a Synchronization Model if and only if it appears sequentially consistent to all software that obey the Synchronization Model.
One way to implement the Synchronization Model is data race free.
Data race free later became the basis of the Java and C ++ memory models.
Java's memory model first appeared in 1995, but since 1997, this memory model has been found to have many serious errors and defects, which hinders many optimization measures, the security of the program is not guaranteed. JSR 2001 was established in 133 and led by William put. Members of the Expert Group include adve, Doug Lea, and William put. JSR 2004 final version was released on JSR 133. Manson Jeremy, William put, and Sarita v. adve has published the Java memory model, which describes the latest Java memory model. This memory model has been introduced in Java 5.0 and has been used since now.
The key to the Java memory model is: if the multi-threaded program meets the data race free requirements, the memory model ensures that the program execution results are the same as those in the sequentially consistent model. In addition, the complexity of the Java memory model lies in that, to ensure program security, we must restrict the multi-threaded program to a certain extent even if it does not satisfy data race free, such restrictions must be appropriate, and too strong will hinder reasonable optimization. Too weak will not ensure the security of the program.
Hans-J. boehm and Sarita v. adve published an article foundations of the C ++ concurrency memory model, describing the basis of the C ++ memory model, this memory model provides clear semantics for threads in the C ++ 11 standard.
C ++ Memory Model and the key lies in: If the multi-threaded program meets the data race free requirements, the memory model ensures that the program execution results are the same as those in the sequentially consistent model. Unlike the Java memory model, the C ++ memory model does not provide any guarantee for the results of multithreaded programs that do not meet the requirements of data race free. In addition, the C ++ memory model provides some features to implement different memory models.