Atomic statements
The concurrent programming abstraction has been defined in terms of the interleaving
of atomic statements. What this means is that an atomic statement is executed
to completion without the possibility of interleaving statements from another
process. An important property of atomic statements is that if two are executed
simultaneously
, the result is the same as if they had been executed sequentially
(in either order). The inconsistent memory store shown on page 15 will not occur.
It is important to specify the atomic statements precisely, because the correctness of an algorithm depends on this specification. We start with a demonstration of the effect of atomicity on correctness, and then present the specification used in this book.
Recall that in our algorithms, each labeled line represents an atomic statement. Consider the following trivial algorithm:
There are two possible scenarios:
In both scenarios, the final value of the global variable n is 2, and the algorithm is a correct concurrent algorithm with respect to the postcondition \(n = 2\).
Now consider a modification of the algorithm, in which each atomic statement references the global variable n at most once:
There are scenarios of the algorithm that are also correct with respect to the post- condition \(n = 2\):
As long as p1 and p2 are executed immediately one after the other, and similarly for q1 and q2, the result will be the same as before, because we are simulating the execution of \(n\leftarrow n+1\) with two statements. However, other scenarios are possible in which the statements from the two processes are interleaved:
Clearly, Algorithm 2.4 is not correct with respect to the postcondition\(n = 2\).
We learn from this simple example that the correctness of a concurrent program is relative to the specification of the atomic statements. The convention in the book is that:
Assignment statements are atomic statements, as are evaluations of boolean conditions in control statements. |
---|
This assumption is not at all realistic, because computers do not execute source code assignment and control statements; rather, they execute machine-code instructions that are defined at a much lower level. Nevertheless, by the simple expedient of defining local variables as we did above, we can use this simple model to demonstrate the same behaviors of concurrent programs that occur when machine-code instructions are interleaved. The source code programs might look artificial, but the convention spares us the necessity of using machine code during the study of concurrency. For completeness, Section 2.8 provides more detail on the interleaving of machine-code instructions.