18. Concurrency
A program that downloads ten files one after another spends most of its time waiting. A program that does them all at once finishes in roughly the time of the slowest. Modern machines also have many CPU cores, and a program that only uses one leaves the rest idle. Concurrency means doing several things at the same time. It is powerful and it is also where the hardest bugs live, so this chapter covers both the tools and the rules for using them safely.
In this chapter you will learn:
asyncfunctions, futures andawait,- the worker pool and
GRAV_MAXPROCS, - what happens to exceptions in background work,
- sharing data safely:
Mutex,AtomicInt,Channel, - dedicated threads and the other synchronisation tools,
- finding data races with
--race.
async and await
Mark a function async and every call to it runs in the background. The call returns at once with a Future<T>, a ticket for a result that is not ready yet. await waits for the result:
import "std"
async fn slowSquare(x: int) -> int {
Async.sleepMs(100) // pretend to do slow work
return x * x
}
fn main() {
let sw = new Stopwatch()
let a = slowSquare(3) // starts now
let b = slowSquare(4) // starts now too
print(await a + await b) // wait for both
print("both in parallel: ${sw.elapsedMs() < 190}")
}25
both in parallel: trueEach call took 100 ms, but both ran at the same time, so the total is about 100 ms, not 200.
awaitworks in any function, async or not.- A
Future<T>is an ordinary value: keep it in a variable, a field or a collection, and await it later. Awaiting it again gives the same result. - Arguments are copied into the task when it starts.
To run many tasks and collect their results, keep the futures in an array:
import "std"
async fn cube(x: int) -> int { return x * x * x }
fn main() {
let jobs = new Array<Future<int>>()
for (i in 1..=5) { jobs.push(cube(i)) }
let results = awaitAll::<int>(jobs)
print(results)
}[1, 8, 27, 64, 125]When main returns, the program waits for any tasks still running before it exits.
Methods can be async too, static ones included. The task gets the object (self) along with the arguments:
import "std"
class Downloader {
private host: string
constructor(host: string) { self.host = host }
async fn fetch(path: string) -> string {
Async.sleepMs(50) // pretend to wait for the network
return self.host + path
}
}
fn main() {
let d = new Downloader("example.com")
let a = d.fetch("/a")
let b = d.fetch("/b")
print(await a + " " + await b)
}example.com/a example.com/bThe worker pool
Tasks run on a pool of worker threads. The pool's size is GRAV_MAXPROCS:
| | |
|-|-|
| default | the number of CPU cores |
| environment | GRAV_MAXPROCS=4 ./app |
| in code | Async.setMaxProcs(4) |
| asking | Async.maxProcs(), Async.pending(), Async.cpuCount() |
For work that mostly waits (network, disk), a pool larger than the number of cores is fine. For work that computes, more workers than cores only adds overhead.
A task that is awaited before any worker has started it runs right there, on the thread that awaits it. So tasks can start and await other tasks without ever deadlocking, even with a single worker:
import "std"
async fn fib(n: int) -> int {
if (n < 15) { return plainFib(n) }
let a = fib(n - 1)
let b = fib(n - 2)
return await a + await b
}
fn plainFib(n: int) -> int { return n < 2 ? n : plainFib(n - 1) + plainFib(n - 2) }
fn main() {
Async.setMaxProcs(1)
print(await fib(20))
}6765Exceptions in tasks
An exception thrown inside a task is kept, and thrown again by await, so you handle it where you collect the result:
import "std"
async fn parse(s: string) -> int {
if (!Format.isInt(s)) { throw new ValueError("not a number: '${s}'") }
return Format.toInt(s)
}
fn main() {
let good = parse("42")
let bad = parse("forty-two")
print(await good)
try {
print(await bad)
} catch (e: ValueError) {
print("caught: ${e.message()}")
}
}42
caught: not a number: 'forty-two'A task that fails and is never awaited is reported when the program exits, like an uncaught exception.
Sharing data safely
When two tasks change the same data at the same time, updates can be lost. count += 1 reads the count, adds one and writes it back; if two threads read the same old value, one increment disappears. This is a data race, and it is nasty because it happens only sometimes.
The safest design shares nothing: pass data into a task, get a result out. When tasks must share changing state, protect it with one of the tools below. (Grav's own runtime, reference counting included, is thread-safe; it is your shared objects that need protecting.)
Mutex: one at a time
A Mutex (mutual exclusion lock) lets only one thread at a time run the code it guards. withLock takes the lock, runs a function and releases the lock, even if the function throws:
import "std"
class Account {
public balance: int
constructor() { self.balance = 0 }
}
async fn deposit(m: Mutex, acct: Account, times: int) {
for (i in 0..times) {
m.withLock(fn() { acct.balance += 1 })
}
}
fn main() {
let m = new Mutex()
let acct = new Account()
let tasks = new Array<Future<void>>()
for (t in 0..8) { tasks.push(deposit(m, acct, 1000)) }
for (t in tasks) { await t }
print(acct.balance)
}8000Without the mutex, the result would be some random number below 8000.
AtomicInt: a counter without a lock
For a single counter, AtomicInt uses the processor's atomic instructions, which is simpler and faster than a lock:
import "std"
async fn countEvens(hits: AtomicInt, lo: int, hi: int) {
for (i in lo..hi) {
if (i % 2 == 0) { hits.increment() }
}
}
fn main() {
let hits = new AtomicInt(0)
let a = countEvens(hits, 0, 5000)
let b = countEvens(hits, 5000, 10000)
await a
await b
print(hits.get())
}5000Channel: passing values between tasks
A Channel<T> is a queue that one task puts values into and another takes them out of. send waits while the channel is full and receive waits while it is empty, so the two sides keep pace with each other. close() says no more values are coming:
import "std"
async fn producer(ch: Channel<string>) {
for (w in ["alpha", "beta", "gamma"]) { ch.send(w) }
ch.close()
}
async fn consumer(ch: Channel<string>) -> string {
let sb = new StringBuilder()
while (ch.hasNext()) {
sb.append(Str.upper(ch.receiveOr(""))).append(" ")
}
return sb.toString()
}
fn main() {
let ch = new Channel<string>(2) // holds at most 2 values
let c = consumer(ch)
let p = producer(ch)
await p
print(await c)
}ALPHA BETA GAMMA Channels turn "shared data" into "messages", which is usually much easier to get right: each value belongs to one task at a time.
Threads
async tasks share a pool and suit short pieces of work. A Thread is a dedicated operating-system thread, for long-running or blocking work such as a server's accept loop or a background worker:
import "std"
fn main() {
let total = new AtomicInt(0)
let wg = new WaitGroup()
for (i in 0..4) {
wg.add(1)
Thread.spawn(fn() {
for (k in 0..1000) { total.increment() }
wg.done()
}).detach()
}
wg.wait()
print("total: ${total.get()}")
let failing = Thread.spawn("worker", fn() { throw new ValueError("disk full") })
print("worker: " + failing.joinResult())
}total: 4000
worker: ValueError: disk fullThread.spawn(body)starts a thread;join()waits for it to finish, and throws aStateErrorif the body failed;joinResult()returns the error message instead (or"");detach()lets it run on its own.- An exception, or a panic, inside a thread stops that thread only.
The other tools in std/concurrency:
| Type | For |
|---|---|
WaitGroup | waiting until a group of threads has finished: add(n), done(), wait() |
CountDownLatch | a gate that opens after n calls to countDown() |
Semaphore | limiting how many threads do something at once: withPermit(body) |
RWLock | many readers or one writer: withRead(body), withWrite(body) |
Once | running an initialiser exactly once: once.run(body) |
A closure that assigns a captured local shares it with the code around it (chapter 6), but nothing guards such a variable: across threads, share state through the types above, which makes the shared parts easy to spot.
Finding data races
Build with --race to have ThreadSanitizer watch every memory access. When two threads touch the same data without synchronisation, it reports both places:
grav app.grav --race --runWARNING: ThreadSanitizer: data race (pid=4242)
Read of size 4 at 0x7208... by thread T1:
#0 vf_bump app.grav:12Run your tests with --race whenever you add concurrency.
Guidelines
- Prefer giving each task its own data and returning a result over sharing.
- Use a
Channelto stream values from one task to another. - Protect shared state with a
Mutex, or anAtomicIntfor counters. Keep the locked code short. - Treat objects you hand to a task as off-limits until you have awaited it.
- Test with
--race.
Summary
- Calling an
async fnstarts a task and returns aFuture<T>;awaitgets the result. - Tasks run on a pool of
GRAV_MAXPROCSworkers; awaiting never deadlocks the pool. - Exceptions in a task are rethrown by
await. Mutex,AtomicIntandChannelmake sharing safe;Threadgives a dedicated thread, withWaitGroup,Semaphoreand friends.--racefinds data races.
Exercises
- Write an
async fnthat sums the numbers in a range, split 1..10,000,000 into four ranges, and add the four results. - Remove the
Mutexfrom the bank example and run it a few times. Then run it with--race. - Build a pipeline: one task sends the numbers 1 to 20 into a channel, a second squares them into another channel, and
mainprints the results. - Use a
Semaphore(2)so that at most two of five threads print "working" at the same time.
Next: Testing →