Further clarification required regarding difference between unbuffered channel (i.e. capacity 0) vs buffered channel of capacity 1 in Golang

huangapple go评论102阅读模式
英文:

Further clarification required regarding difference between unbuffered channel (i.e. capacity 0) vs buffered channel of capacity 1 in Golang

问题

在下面的链接中,关于无缓冲通道和容量为1的缓冲通道之间的区别,有一个回答是这样说的:“如果通道是无缓冲的(容量为零),那么只有在发送方和接收方都准备好的情况下,通信才会成功”。

当作者说发送方和接收方都准备好时,这究竟是什么意思?从时间上来说,我可以说其中一个必须在另一个之前吗?如果是这样,我可以说接收方必须先准备好,然后才能发送吗?

我一直在官方和非官方渠道上寻找解释。然而,我还没有找到一个令人满意的答案。我找到的最接近的解释如下:

这是否意味着在 ch <- "C" 之下的任何代码(如果容量为3)仍然会运行?回到我最初的问题,关于容量为0和1之间的区别,这是否意味着在发送方之下的任何代码都不会运行(对于容量为0)?另一方面,如果容量为1,即使它已经达到了1的最大容量,发送方之下的代码仍然会运行吗?

谢谢!

英文:

In the link below, one of the replies given on the difference between unbuffered vs buffered channel of 1 capacity is that if "the channel is unbuffered (capacity is zero), then communication succeeds only when the sender and receiver are both ready".

What does it mean exactly when the author says the sender and receiver are both ready? Chronologically, am I right to say that one has to come before the other? And if so, am I also right to say that the receiver has to be ready first before the sender?

https://stackoverflow.com/questions/53348202/the-differences-between-channel-buffer-capacity-of-zero-and-one-in-golang

I been trying to look for explanations on both official and unofficial channels. However, I haven't found a satisfactory answer yet. The closest I have come is from the explanation below.

Further clarification required regarding difference between unbuffered channel (i.e. capacity 0) vs buffered channel of capacity 1 in Golang

Does this mean that any code written below ch <- "C" (if capacity = 3) will still run? Going back to my original question on the difference between capacity 0 vs 1, does this mean that any code below the sender will not run (for capacity = 0)? On the other hand, if capacity = 1, the code below the sender will still run so long even if it is at full capacity of 1?

https://www.golinuxcloud.com/golang-buffered-channel/

Thank you!

答案1

得分: 1

当作者说发送方和接收方都准备好时,它的确意味着一个goroutine正在执行接收操作,而另一个goroutine正在执行发送操作。不管哪个goroutine首先开始操作都没有关系。

换句话说,发送的goroutine会阻塞,直到另一个goroutine在通道上进行接收。

与具有可用容量的缓冲通道相比,这与无缓冲通道形成对比。一个goroutine可以在不等待另一个goroutine接收的情况下完成对通道的发送。

下面是一个示例,说明了无缓冲通道和缓冲通道之间的区别。

sleepRecv := func(c chan int) {
	time.Sleep(time.Second)
	<-c
}

b0 := make(chan int, 0)
go sleepRecv(b0)
start := time.Now()
b0 <- 0                        // 必须等待goroutine中的接收操作
fmt.Println(time.Since(start)) // 输出 1s

b1 := make(chan int, 1)
go sleepRecv(b1)
start = time.Now()
b1 <- 0                        // 不需要等待goroutine中的接收操作
fmt.Println(time.Since(start)) // 输出 0s
英文:

> What does it mean exactly when the author says the sender and receiver are both ready?

It means that one goroutine is executing receive and that another goroutine is executing send. It does not matter which goroutine begins the operation first.

To put this another way, a sending goroutine blocks until another goroutine receives on the channel.

Contrast this with a buffered channel with available capacity. A goroutine can complete send to the channel without waiting for another goroutine to receive on the channel.

Here's an example that illustrates the difference between an unbuffered and buffered channel.

sleepRecv := func(c chan int) {
	time.Sleep(time.Second)
	&lt;-c
}

b0 := make(chan int, 0)
go sleepRecv(b0)
start := time.Now()
b0 &lt;- 0                        // must wait for recv in goroutine
fmt.Println(time.Since(start)) // prints 1s

b1 := make(chan int, 1)
go sleepRecv(b1)
start = time.Now()
b1 &lt;- 0                        // does not wait for recv in goroutine
fmt.Println(time.Since(start)) // prints 0s

huangapple
  • 本文由 发表于 2023年2月25日 00:06:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75558785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定