WaitGroup and Worker Pool

A worker pool runs a fixed number of workers over a queue of jobs. A WaitGroup lets main wait until all jobs are done.

The idea

A supermarket has 3 checkout lanes (workers) and a line of customers (jobs). When a lane frees up, it takes the next customer. You do not open 1000 lanes for 1000 customers; you reuse a few.

The manager needs to know when everyone has been served. A sync.WaitGroup is a counter: Add(1) for each job, Done() when one finishes, and Wait() blocks until the counter reaches zero.

Watch it work

Try it

Common mistakes