Answer the question
In order to leave comments, you need to log in
How do buffered and unbuffered channels work?
Good afternoon. Can anyone explain the difference between a blocking channel that is created by simply declaring a type: chan bool
, and a non-blocking (buffered) channel that is created with make(chan bool, n) - где n >= 1
1. What exactly is being blocked, the entire thread or the goroutine ? I'm assuming it's a goroutine!?
2. What happens with a buffered channel!? All data transferred to this channel is stored in some buffer and the goroutine is not blocked like that!?
Answer the question
In order to leave comments, you need to log in
An unbuffered channel will block the throat until nothing is read from it.
A buffered channel will block the goroutine only if the entire buffer is full AND another write is attempted. Once the channel is read, the goroutine is unlocked.
If there is only one goroutine (only the main function) and the channel blocks it, the program will crash with an error because all goroutines are blocked - there is nothing to execute.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question