I have a queue and a function that does both dequeueing and enqueueing. I want to make sure that the right amount of goroutines operate on the queue, as long as there is som
There's runtime.NumGoroutine but you're approaching this wrong.
One approach is to use a sync.WaitGroup.
func deen(wg *sync.WaitGroup, queue chan int) {
for element := range queue {
fmt.Println("element is ", element)
if element%2 == 0 {
fmt.Println("new element is ", element)
wg.Add(2)
queue <- (element*100 + 11)
queue <- (element*100 + 33)
}
wg.Done()
}
}
func main() {
var wg sync.WaitGroup
queue := make(chan int, 10)
queue <- 1
queue <- 2
queue <- 3
queue <- 0
for i := 0; i < 4; i++ {
wg.Add(1)
go deen(&wg, queue)
}
wg.Wait()
close(queue)
fmt.Println("list len", len(queue)) //this must be 0
}
playground
--- old buggy version with a race in it ---
func deen(wg *sync.WaitGroup, queue chan int) {
for element := range queue {
wg.Done()
fmt.Println("element is ", element)
if element%2 == 0 {
fmt.Println("new element is ", element)
wg.Add(2)
queue <- (element*100 + 11)
queue <- (element*100 + 33)
}
}
}
func main() {
var wg sync.WaitGroup
queue := make(chan int, 10)
queue <- 1
queue <- 2
queue <- 3
queue <- 0
for i := 0; i < 4; i++ {
wg.Add(1)
go deen(&wg, queue)
}
wg.Wait()
close(queue)
fmt.Println("list is has len", len(queue)) //this must be 0
}
playground