15 Concurrency and parallelism
Page 1 of 9
A program that asks a service a question waits for the answer. When it asks three warehouses how many lamps they hold, one after another, it waits three times, although none of the questions needs the answer of another. This chapter shows how to ask them all at once. You learn what it means for work to run at the same time, how to write it with together and together for, and exactly what Una promises about the result: what it can depend on, what happens when a warehouse does not answer, and in what order the effects are recorded. You end with a stock check that asks every warehouse about every line of an order at once.
What you'll build
A stock check for a store with three warehouses, north, south and east. For every line of an order, it asks all three warehouses at the same time how many units they hold, and it checks all the lines at once too. It prints what each warehouse holds, whether the order ships, and which warehouse did not answer when one is offline:
Order 1042
LAMP-01: 5 wanted, 4 + 2 + 0 = 6 on hand, ships
DESK-02: 2 wanted, 1 + 0 + 1 = 2 on hand, ships
the whole order ships
Order 1043
RUG-07: 6 wanted, 0 + 3 + 5 = 8 on hand, ships
LAMP-01: 9 wanted, 4 + 2 + 0 = 6 on hand, short by 3
the order waits for stock
Order 1042, again
the east warehouse did not answer; check again laterYou will learn to:
- run a few pieces of work at the same time, each bound to a name, with
together; - build a list with one piece of work per element, all at the same time, with
together for; - predict the result of concurrent work: the result of running it one piece after another;
- know which failure wins when several pieces fail, and in what order effects are recorded;
- test concurrent code with fakes, and read U2023 and the other diagnostics of this chapter.
Download this chapter's packages: each step is a package under steps/, and the chapter's program is day-to-day/; run una check . and una run . in one.
Why it works this way
Running work at the same time is one of the hardest parts of programming in most languages. The trouble comes from two places. When two pieces of work change the same value, such as a counter, the result depends on which one gets there first: one update can be lost, and only on some runs. And when the result depends on timing, a test can pass a hundred times and fail on the next run, on another computer, or under load. Such bugs are hard to reproduce, and so hard to fix.
Una allows concurrency in one shape only, and that shape removes both sources. A piece of work that runs alongside others can read the values around it but cannot change them, so no two pieces ever change the same value. Every piece ends before the block that started it ends, so nothing keeps running in the background. And the result of the block is fixed: it is the result of running the pieces one after another, in the order they are written, stopping at the first failure. Running them at the same time can change how long the program waits, and the order in which the outside world sees their effects, but never what the program computes.
In Una, work that runs at the same time gives the result of running it one piece after another, in the order written: concurrency changes when things happen, not what the program computes.