Following Marcio's thread pool implementation is it possible to determine when all work is done?
Waiting for the JobQueue to empty is trivial:
// Wait for the job queue to clear
for len(JobQueue) > 0 {
// Just wait
}
However after that there might still be goroutines waiting for workers, or workers have not unfinished all tasks:
func (d *Dispatcher) dispatch() {
for {
select {
case job := <-JobQueue:
// a job request has been received
go func(job Job) {
// try to obtain a worker job channel that is available.
// this will block until a worker is idle
jobChannel := <-d.WorkerPool
// dispatch the job to the worker job channel
jobChannel <- job
}(job)
}
}
}
What's the best approach to this? Add a WaitGroup to the dispatcher, and a method for querying the status of the WaitGroup? Any pointers towards this would be appreciated.
Per the documentation:
A WaitGroup waits for a collection of goroutines to finish.
So, yes, to wait for a collection of goroutines to finish, I would recommend a WaitGroup
. As the docs state, you call Add
for each worker started, and Done
when each worker finishes. Your for len()
busy loop would be replaced with:
wg.Wait()
As per the documentation.