Is it possible to use Go's buffered channel as a thread-safe queue?

前端 未结 3 622
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 11:34

I want to find a queue structure (a data container) whose elements must be first-in-first-out. It is important for me that the structure must be thread-safe. I\'m going to u

相关标签:
3条回答
  • 2020-12-10 12:13

    In Go, a buffered channel is just that: a thread-safe FIFO queue so what you are trying to do is perfectly valid. You shouldn't have performance issues at all with this approach.

    0 讨论(0)
  • 2020-12-10 12:14

    I'm pretty sure that Channels are FIFO. They are also cheap so they would be memory efficient. Beyond that without knowing the details of how you are going to use them We can't really give much more advice.

    0 讨论(0)
  • 2020-12-10 12:27

    In general, I would say buffered channels do not make a good concurrency-safe queue. Creating them allocates memory for the entire buffer. If your queue size varies from very small to very large during execution, you have to allocate for the worst case scenario and may be wasting a lot of memory.

    0 讨论(0)
提交回复
热议问题