I need to start a huge amount of goroutines and wait for their termination. The intuitive way seems to use a channel to wait till all of them are finished :
package main
type Object struct {
//data
}
func (obj *Object) Update(channel chan int) {
//update data
channel <- 1
return
}
func main() {
channel := make(chan int, n)
list := make([]Object, n, m)
for {
for _, object := range list {
go object.Update(channel)
}
for i := 0; i < n; i++ {
<-channel
}
//now everything has been updated. start again
}
}
But the problem is that the amount of objects and therefore the amount of goroutines could change. Is it possible to change the buffer size of a channel ?
Is there maybe a more elegant way to do this ?
The WaitGroup type of sync package, is used to wait for the program to finish all goroutines launched from the main function. It uses a counter that specifies the number of goroutines, and Wait blocks the execution of the program until the WaitGroup counter is zero.
To invoke this function in a goroutine, use go f(s) . This new goroutine will execute concurrently with the calling one. You can also start a goroutine for an anonymous function call. Our two function calls are running asynchronously in separate goroutines now.
A common usage of WaitGroup is to block the main function because we know that the main function itself is also a GoRoutine. You need to import the sync package before you can use WaitGroup. WaitGroup is of struct type and it has three functions, which you can use: Add(int), Wait() and Done().
In Go, we use goroutines to create concurrent programs. Concurrent programs are able to run multiple processes at the same time. Suppose we have a program that has two independent functions. In normal programs, two functions will be executed synchronously.
I've used WaitGroup as a solution to this problem. Translating your current code, with some logs to make it clear what's happening:
package main
import "sync"
import "fmt"
import "time"
type Object struct {
//data
}
func (obj *Object) Update(wg *sync.WaitGroup) {
//update data
time.Sleep(time.Second)
fmt.Println("Update done")
wg.Done()
return
}
func main() {
var wg sync.WaitGroup
list := make([]Object, 5)
for {
for _, object := range list {
wg.Add(1)
go object.Update(&wg)
}
//now everything has been updated. start again
wg.Wait()
fmt.Println("Group done")
}
}
This task in not exactly trivial, it's quite easy to write a buggy one. I recommend to use a ready made solution in the stdlib - sync.WaitGroup
. Quoting from the link:
A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With