I have a slice of struct that defines a task, each task is run in a goroutine, and I want all go goroutines to stop whenever the first one complete a task via the signal task.signalComplete
Currently I have the following.
for _, task := range taskList {
go func(task *myTask, firstCompleteSignal chan<- bool) {
for {
select {
// When the task completes, it emit signalComplete
case <-task.signalComplete:
firstCompleteSignal<-true
return
}
}
}(task, firstCompleteSignal)
}
for {
select {
case <-firstCompleteSignal:
// manually stop all go thread
return
}
}
Is this canonical?
Or is there library to do this for me like sync.WaitGroup for waiting all goroutine to be done?