doula2426 2014-09-11 18:57
浏览 16
已采纳

缓冲通道是否维持秩序?

In Go, do buffered channels have any order guarantee?

For example: you have two goroutines A & B that share a channel. A pushes data onto the channel while B reads from it. Are you guranted that B will read data In the same order that A put it into the channel?

I understand that if there are multiple producers or consumers the order may be non-deterministic, but I'm specifically asking about having just 1 producer and 1 consumer.

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-09-11 19:03
    关注

    You can see the idea of channel illustrated in "The Nature Of Channels In Go": it shows how the order or read/write is respected.
    See also 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.

    Unbuffered Channels

    http://3.bp.blogspot.com/-vnJIWvlbP-E/UwDVICJKB9I/AAAAAAAANX0/T04V_58i8Vs/s1600/Screen+Shot+2014-02-16+at+10.10.54+AM.png

    Buffered Channel

    http://1.bp.blogspot.com/-GkVAtGeUzrs/UwIQ6AezJmI/AAAAAAAANYE/5XWpxN-zA3w/s1600/Screen+Shot+2014-02-17+at+8.38.15+AM.png

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看