Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waiit for the "run" routine to return on Close #5

Merged
merged 2 commits into from
Feb 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (wp *WorkerPool) run() {
<-wp.workers
}()
}
close(wp.workers)
}

// Submit submits f for processing by a worker. The given id is useful for
Expand Down Expand Up @@ -120,6 +121,9 @@ func (wp *WorkerPool) Drain() ([]Task, error) {

wp.wg.Wait()

// It's not necessary to hold a lock when reading wp.results as no other
// routine is running at this point besides the "run" routine which should
// be waiting on the tasks channel.
res := make([]Task, len(wp.results))
for i, t := range wp.results {
res[i] = t
Expand Down Expand Up @@ -150,5 +154,8 @@ func (wp *WorkerPool) Close() error {
// At this point, all routines have returned. This means that Submit is not
// pending to write to the task channel and it is thus safe to close it.
close(wp.tasks)

// wait for the "run" routine
<-wp.workers
return nil
}