0

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

bemeyer
  • 6,154
  • 4
  • 36
  • 86

1 Answers1

2

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.

sberry
  • 128,281
  • 18
  • 138
  • 165
  • Oh i actually see the issue that i didn't loop at all which is kind of a simple mistake. How does this range queue work here? Since the channel will be filled "step by step" ? – bemeyer Feb 11 '18 at 18:04
  • If the channel is not closed, this would block forever on an empty channel. That is why it is important that you close the channel like you do. – sberry Feb 11 '18 at 18:05
  • thank you very much that does explain it. Didn't know that a range on a channel will be simelar to a while "wait till its closed". The missing loop is obvious too. – bemeyer Feb 11 '18 at 18:08