Search code examples
gogoroutine

Using a channel for dispatching tasks to go routine


I am writing a program to render diagrams. Todo so I'm searching for all files and want to dispatch them async to go routines to process them in parallel. But I think I misunderstood the concept of channels.

files := umlFiles("uml") // list of strings

queue := make(chan string)
for i := 0; i < 4; i++ {
    go func(queue chan string) {
        file, ok := <-queue
        if !ok {
            return
        }

        exec.Command("some command", file).Run()
    }(queue)
}
for _, f := range files {
    queue <- f
}
close(queue)

This will run in a deadlock after it is done with the first 4 files but never continues with the rest of the files.

Can i use a channel to dispatch tasks to running go routines and stop them when all of the tasks are done? If so what is wrong with the code above?

Used to get here:
how-to-stop-a-goroutine
go-routine-deadlock-with-single-channel


Solution

  • You are very close. The problem you have is you are launching 4 goroutines which all do 1 piece of work, then return. Try this instead

    for i := 0; i < 4; i++ {
        go func(queue chan string) {
            for file := range queue {
                exec.Command("some command", file).Run()
            }
        }(queue)
    }
    

    Once queue is closed these will return.