Try, Check, Try Again
Last timeFour Things Waiting in a Circle
Instead of a door that admits one, the machine offers an indivisible change-if-unchanged operation. Build on it and nobody waits, but somebody has to repeat their work.
The lock in the earlier lessons solves the problem by making everybody take
turns, and the bill arrives as waiting. There is another approach entirely, and
it rests on one operation the processor gives you.
The primitive
The machine offers an operation that does three things indivisibly: compare a
memory location against a value you expected, and if they are equal, write a new
value, and tell you whether it did. Nothing can occur between the comparison and
the write.
That is enough. If you read a value, compute what it should become, and then
attempt this operation with your old reading as the expectation, you commit your
change only if nothing happened in between. If something did happen, the
operation refuses and tells you so.
Nobody waits, somebody repeats
The appeal is obvious. A worker that loses the race does not sleep, is not woken,
and does not depend on another worker being scheduled. If a holder of a lock is
suspended by the operating system at the worst moment, everybody behind it waits
for it to be scheduled again. Here there is no holder, so that cannot happen.
The lesson stops here
6 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents