Grokking Concurrency in Go
0% completed
Cancellation with WithCancel
Output:
worker 0 stopped after 2 steps: context canceled worker 1 stopped after 2 steps: context canceled worker 2 stopped after 2 steps: context canceled
Three goroutines run the same loop, and main stops all of them with one call to cancel. Each worker finishes two 40 ms steps, at 40 ms and 80 ms, and is partway through its third when main calls cancel at 100 ms. The ctx.Done() case is then ready, so the worker reports and returns. Each worker writes only its own element of reports, and wg.Wait makes main wait until all three have returned.
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%