Inverted worker thread pool pattern from Rethinking Classical Concurrency Patterns
var sem = make(chan struct{}, limit)
func start() {
for _, task := range hugeSlice {
sem <- struct{}{}
go func(task Task) {
defer func() { <-sem }()
perform(task)
}(task)
}
}
func wait() {
for n:= limit; n > 0; n-- {
sem <- struct{}{}
}
}
This pattern could be extended to reduce goroutine creation overhaed. Make hugeSlice
a channel and have the goroutines consume from it until it's empty.
Alternatively the x/sync/semaphore can be used.