I'm not sure what I'm missing but I get a deadlock error. I'm using a buffered channel that I range over after all go routines complete. The channel has the capacity of 4 and I'm running 4 go routines so I'm expecting it to be "closed" automatically once it reaches the max capacity.
package main
import "fmt"
import "sync"
func main() {
ch := make(chan []int, 4)
var m []int
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
ch <- m
return
}()
}
wg.Wait()
for c := range ch {
fmt.Printf("c is %v", c)
}
}
You have two problems :
range ch
is still waiting for elements to come in the channel and there's no goroutine left to write on it.Solution 1 :
Make the channel big enough and, close it so that range
stops waiting :
ch := make(chan []int, 5)
...
wg.Wait()
close(ch)
Demonstration
This works but this mostly defeats the purpose of channels here as you don't start printing before all tasks are done.
Solution 2 :
This solution, which would allow a real pipelining (that is a smaller channel buffer), would be to do the Done()
when printing :
func main() {
ch := make(chan []int, 4)
var m []int
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
ch <- m
return
}()
}
go func() {
for c := range ch {
fmt.Printf("c is %v\n", c)
wg.Done()
}
}()
wg.Wait()
}
Demonstration
I'm running 4 go routines
No, you're running 5 - thus the deadlock, as the channel buffers only 4 messages.
However, iterating over the channel will later on deadlock the program too. Since the channel isn't closed, it'll block forever once it read your 5 values.
So,
ch := make(chan []int, 5)
and
close(ch)
before the range
loop.
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