In Golang unbuffered channel is just a FIFO queue. How many items can be in that queue at any time? Is there a limit?
Asked
Active
Viewed 2,850 times
2
-
1Unbuffered means no buffer (zero). See [Golang - What is channel buffer size?](http://stackoverflow.com/questions/11943841/golang-what-is-channel-buffer-size). – e0k Jan 02 '17 at 18:15
-
I get that, however unbuffuered vs buffered property only affects **blocking** of the execution, rather than number of things that get added to the queue. If I keep calling a go routine concurrently that sends to the unbuffered channel, I will still see all of the messages processed in sequential order, none of the messages are getting lost - from what I can see – smokeybear Jan 02 '17 at 18:18
-
Is your question how many blocked goroutines can be waiting to write to an unbuffered channel? – e0k Jan 02 '17 at 18:28
-
@e0k most definitely yes! and what order routines will be processed in? – smokeybear Jan 02 '17 at 18:29
-
1If you have one goroutine writing to a channel and one reading from it, then they will come out in the same order that you put them in. But if you have multiple goroutines writing to the channel, you can not predict the order that they go in (i.e. without further synchronization). This unpredictability is the nature of concurrency. You cannot (or should not) assume any particular order of concurrent operations. – e0k Jan 02 '17 at 18:33
-
1just a little tip don't think that a buffered channel is a queue – Yandry Pozo Jan 02 '17 at 20:45
1 Answers
4
The number of items that can be in the channel itself is zero, because it is unbuffered. But there is no limit on the number of goroutines than can be waiting to send on the channel. (When a goroutine tries to send on a channel with no buffer or a full buffer, it blocks until another goroutine is ready to receive from the channel.)

andybalholm
- 15,395
- 3
- 37
- 41
-
Will accept the answer as it makes most sense, however is the number of goroutines truly unilmited? – smokeybear Jan 02 '17 at 18:19
-
Also in this case, if channel was unbuffered will goroutines be served in FIFO order as well? or is it random? – smokeybear Jan 02 '17 at 18:20
-
1See [Max number of goroutines](http://stackoverflow.com/questions/8509152/max-number-of-goroutines). There is no language-imposed limit on the number of goroutines, but since each uses a small amount of memory, this imposes a physical limit. – e0k Jan 02 '17 at 18:40
-
The blocked goroutines are unblocked in FIFO order as receivers become available. – andybalholm Jan 02 '17 at 19:46