doulei3488 2016-05-04 15:50
浏览 257

golang fifo缓冲通道

from my understanding: buffered channels in GO are not FIFO when the channel is full.
I need this behaviour in my application (FIFO behaviour).
How can I achieve that behaviour? Is there any open source for that?
Thanks in advance

EDIT:
some people disliked the question so let me be more clear:
I meant that when a buffered channel is full and multiple senders are blocked
while trying to add items to the channel the order in which they'll be released
is not FIFO. You can also read this discussion: https://github.com/golang/go/issues/11506

So yeah, I was looking for a third party library that implements that behaviour.
Sorry for not being clear.

  • 写回答

1条回答 默认 最新

  • dongye4192 2016-05-04 16:05
    关注

    Buffered channels in Go are always FIFO. The specification clearly says:

    Channels act as first-in-first-out queues.

    If the values coming out of the channel are not FIFO, then this is a bug in the channel implementation.

    The following code should always print 1, 2, 3, 4 in this correct order:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        ch := make(chan int, 3)
        ch <- 1
        ch <- 2
        ch <- 3
    
        go func() {
            ch <- 4
        }()
    
        time.Sleep(time.Second)
    
        for i := 0; i < 4; i++ {
            fmt.Println(<-ch)
        }
    }
    

    Playground link

    Please note, that there is no guaruantee which value will be send first when there are multiple concurrent senders. If there are multiple waiting senders and someone removes one element from the channel buffer (or in the case of an unbuffered channel, tries to receive from the channel) the runtime will randomly choose one of the sending goroutines.

    Example:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        ch := make(chan int, 2)
        ch <- 1
    
        go func() {
            ch <- 2
        }()
    
        go func() {
            ch <- 3
        }()
    
        time.Sleep(time.Second)
    
        for i := 0; i < 3; i++ {
            fmt.Println(<-ch)
        }
    }
    

    Playground link

    If you run this code multiple times, you can see that the output will sometimes either be 1, 2, 3 or 1, 3, 2. (This doesn't work on the playground, as the output is cached)

    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了