list all elements in a buffered channel

若如初见. 提交于 2019-12-06 03:49:41

问题


Is there a (non destructive) way to list all the element in a buffered channel?

The only thing I can think about is to cycle all of them, reinserting them at the end. This doesn't seem the smartest approach.

Link to playground

c := make(chan int, 100)
c <- 111
c <- 222
for i:=0;i<2;i++ {
element := <- c
fmt.Println(element)
c <- element
}
fmt.Println(len(c))

回答1:


This thread from 2011 offered some wrapper around a channel in order to enable a Peek() function, but that was more a workaround than anything else.

type PeekChanInt struct {
        in <-chan int
        out chan int
}

The general conclusion was:

A synchronous channel has no head (it's like a zero-length slice)

You can't do that because

  • a) it gets put back at the tail of the queue rather than the end and
  • b) a writer may have got there first, so the put may block.

I have wanted a feature like this in the past. It can make sense when there is only one consumer of the channel (I wanted it to peek at the first mouse event to do hit testing before deciding whether to consume it)

You can simulate it with a process acting as intermediary, but you'd have to do it for each channel type or lose type safety.

Remember there is no buffering, so if you peek at a value, you'd have to get the value from the other side, which would be equivalent to reading it - but that's wrong because peeking should have no side effects.




回答2:


No you can't, you can write your own blocking queue based on a list if you want to do that.




回答3:


You can't really. What you have is the only way, but don't do that if there's any concurrent access to the chan (and then why are you using a chan?).

Items could be inserted or removed after you check len, and during your for loop.



来源:https://stackoverflow.com/questions/26045787/list-all-elements-in-a-buffered-channel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!