- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在 Go 中,缓冲 channel 是否有顺序保证?
例如:你有两个 goroutines A & B 共享一个 channel 。 A 将数据推送到 channel 上,而 B 从中读取数据。你保证 B 会按照 A 放入 channel 的相同顺序读取数据吗?
我知道如果有多个生产者或消费者,顺序可能是不确定的,但我特别想问的是只有 1 个生产者和 1 个消费者。
最佳答案
"Are you guaranteed that B will read data In the same order that A put it into the channel?"
是的。保证数据的顺序。
但是交付仅对无缓冲 channel 有保证,没有缓冲。
(请参阅此答案的第二部分)
您可以从 The Nature Of Channels In Go 中看到“William Kennedy”中说明的 channel 概念。 (2014 年 2 月):它显示了如何遵守顺序或读/写。
另见 Channels :
Receivers always block until there is data to receive.
- If the channel is unbuffered, the sender blocks until the receiver has received the value.
- If the channel has a buffer, the sender blocks only until the value has been copied to the buffer; if the buffer is full, this means waiting until some receiver has retrieved a value.
图片来源:Ardan labs - William Kennedy
同一位 William Kennedy 在“ The Behavior Of Channels ”(2017 年 10 月)中详细介绍了交付保证方面的内容
Do I need a guarantee that the signal sent by a particular goroutine has been received?
图片来源:同样,Ardan labs - William Kennedy
The three channel options are Unbuffered, Buffered >1 or Buffered =1.
Guarantee
- An Unbuffered channel gives you a Guarantee that a signal being sent has been received.
- Because the Receive of the signal Happens Before the Send of the signal completes.
No Guarantee
- A Buffered channel of size >1 gives you No Guarantee that a signal being sent has been received.
- Because the Send of the signal Happens Before the Receive of the signal completes.
Delayed Guarantee
- A Buffered channel of size =1 gives you a Delayed Guarantee. It can guarantee that the previous signal that was sent has been received.
- Because the Receive of the First Signal, Happens Before the Send of the Second Signal completes.
关于go - 缓冲 channel 是否维持秩序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25795131/
我有一个非常基本的 java 迭代器场景...在其中面临以下寻找迭代器工作的问题 迭代器 logIterator 在两个 while 循环中是否具有相同的值,或者它会在第二个 while 循环中结束并
我正在开发一个 REACT Web 应用程序。我正在使用react-datasheet库并使用NPM安装。现在为了使其支持 IE11,我对 NPM 安装的 javascript 文件做了一些更改。这适
我正在使用 esp8266 Arduino(通过 Adafruit Feather Huzzah),我试图连续向 TCP 套接字写入 3 个字符,但没有任何连续的内容。它具有非常规则的高低带宽模式。它
我是一名优秀的程序员,十分优秀!