Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for the termination of n goroutines

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 ?

like image 680
lhk Avatar asked May 16 '13 14:05

lhk


People also ask

How do you wait for Goroutines to finish?

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.

How are Goroutines executed?

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.

How do you use Wait groups?

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().

What is the use of Goroutines?

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.


2 Answers

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")
    }
}
like image 173
beatgammit Avatar answered Sep 17 '22 15:09

beatgammit


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.

like image 38
zzzz Avatar answered Sep 19 '22 15:09

zzzz