I have a buffered channel that are read by multiple (4 in this example) go routines.
queue := make(chan string, 10000) // a large buffered channel
Each go routine checks the number of elements available in the channel and process them all.
for i :=0; i< 4; i++{ // spun 4 go routines
go func() {
for {
for elem := range queue {
// do something with the elem from the channel
}
}
}
}
Will multiple go routines collide on the reads? In other words, could different go routine grab the same elem in the channel, or while one go routine is reading the buffer, the other go routines already read and processed some of the elements? How to block other go routines from reading while one go routine is reading?