WaitGroup.done
func WaitGroup.done(wg: WaitGroup): voidWaitGroup.done decrements the counter of wg by one. It panics if the counter is already zero.
Details
- Each call takes one off the counter: one
donefor each unit anaddcounted. - Everything a goroutine writes before its
doneis visible after thewaitthat thedoneunblocks. - A
donewith noaddbefore it, or one more thanaddcounted, panics. - A panic in a goroutine skips a plain
doneat its end, andwaitthen never sees the counter reach zero. Call it in adeferat the top of the goroutine.
Basic examples
Count one goroutine, mark it done when it finishes, and wait for it:
let wg = WaitGroup.new()
wg.add(1)
go {
println("working")
WaitGroup.done(wg)
}
wg.wait()
println("finished")working
finishedPut the done in a defer at the top of each goroutine, so it runs on every way out. Each goroutine writes its own slot, and the main goroutine reads them after the wait:
let wg = WaitGroup.new()
let squares = Array.filled(4, 0)
for i in Array.range(0, 4) {
wg.add(1)
go {
defer { WaitGroup.done(wg) }
squares[i] = i * i
}
}
wg.wait()
println(squares)[0, 1, 4, 9]Scope
One add can count several goroutines at once; each of them calls done once:
let wg = WaitGroup.new()
let names = ["Ann", "Bob", "Cid"]
let greetings = Array.filled(names.length(), "")
wg.add(names.length())
for entry in names.entries() {
go {
defer { wg.done() }
greetings[entry.index] = "hi, ${entry.value}"
}
}
wg.wait()
println(greetings)["hi, Ann", "hi, Bob", "hi, Cid"]done does not have to run on another goroutine. When the counter reaches zero on the goroutine that waits, wait returns at once:
let wg = WaitGroup.new()
wg.add(2)
wg.done()
wg.done()
wg.wait()
println("counter is zero")counter is zeroA wait group is a reference value. Pass it to a function, and that function's done counts down the same counter:
func work(id: int, wg: WaitGroup, out: Array[string]) {
defer { wg.done() }
out[id] = "job ${id} done"
}
let wg = WaitGroup.new()
let out = Array.filled(3, "")
for id in Array.range(0, 3) {
wg.add(1)
go { work(id, wg, out) }
}
wg.wait()
println(out | String.join("\n"))job 0 done
job 1 done
job 2 doneApplications
Close a results channel once every worker is done. A separate goroutine waits for the workers and then closes the channel, which ends the for loop over its values:
let jobs = [3, 1, 4, 1, 5]
let results: Channel[int] = Channel.new()
let wg = WaitGroup.new()
for job in jobs {
wg.add(1)
go {
defer { wg.done() }
results.send(job * 10)
}
}
go {
wg.wait()
results.close()
}
var total = 0
for r in results.values() {
total += r
}
println(total)140Properties and relations
Everything a goroutine writes before its done is visible after the wait that the done unblocks:
type Tally = { a: int, b: int }
let tally = Tally{ a: 0, b: 0 }
let wg = WaitGroup.new()
wg.add(2)
go {
tally.a += 1
wg.done()
}
go {
tally.b += 1
wg.done()
}
wg.wait()
assert(tally.a == 1 && tally.b == 1)Possible issues
A done with no add before it, or one more than add counted, panics:
WaitGroup.done(WaitGroup.new()) // panic: WaitGroup.done: the counter is already zerolet wg = WaitGroup.new()
wg.add(1)
wg.done()
wg.done() // panic: WaitGroup.done: the counter is already zeroA panic before a plain done skips it, so the counter never reaches zero and wait blocks forever. Here a context with a timeout ends the wait instead, and Context.isDone shows that the wait timed out:
let wg = WaitGroup.new()
let ctx = Context.new(timeout: 50ms)
wg.add(1)
go {
let empty: Array[int] = []
println(empty[0])
wg.done()
}
wg.wait(ctx: ctx)
println(ctx.isDone())trueWith defer { wg.done() } at the top of the goroutine, the panic still counts it down, and the wait returns without the context:
let wg = WaitGroup.new()
wg.add(1)
go {
defer { wg.done() }
let empty: Array[int] = []
println(empty[0])
}
wg.wait()
println("not stuck")not stuckSee also
WaitGroup.add · WaitGroup.wait · WaitGroup.new · WaitGroup · Parallel.each · Mutex.unlock · Channel.close · defer · Concurrency guide
