Golang Concurrency Tricks

Golang Concurrency Tricks

Golang concurrency model based on goroutines and channels is not free from sharp edges.

This page intends both (1) to collect helpful guidelines for writing concurrent code in Go and (2) to bring up well known potential issues to attention.

Channel Hints

  • C1. Some channel operations cause runtime panic:

    • P1. Closing the nil channel.
    • P2. Closing a closed channel.
    • P3. Sending on a closed channel.
  • C2. Do not close a channel from a receiver goroutine. Closing the channel from a receiver could make future sender goroutines to panic.

  • C3. If a channel has multiple senders, do not close the channel from a sender goroutine. Closing the channel from a sender could make future sender goroutine to panic.

    Alternatively, coordinate senders so that only the last sender to leave closes the channel (for instance by using either atomic int or sync.WaitGroup)

    Last one sender to leave, turns off the lights, which can be controlled by a atomic int. 
    https://groups.google.com/d/msg/golang-nuts/LM648yrPpck/oZFSD-oMAwAJ
  • C4. It is not required to close an unused channel. If no goroutine is left referencing the channel, it will be garbage collected.

    Note that it is only necessary to close a channel if the receiver is looking for a close. Closing the channel is a control signal on the channel indicating that no more data follows. 
    https://groups.google.com/forum/#!msg/golang-nuts/pZwdYRGxCIk/qpbHxRRPJdUJ
  • C5. Channels work well when enclosed in a 'select'.

    If you are ever using a channel outside of a select in production code, you are probably doing it wrong. 
    https://groups.google.com/d/msg/golang-nuts/LM648yrPpck/j5eHsPc2AwAJ
  • C6. If you need bidirectional communication between two goroutines, consider using two unidirectional channels. Thus both channel sides will be able to use the close idiom to signal termination.

  • C7. Beware: one sender goroutine risks blocking indefinitely when writing on a channel if there is no goroutine left receiving from it.

  • C8. When designing a goroutine which provides service through channels, and at some point a running goroutine is no longer needed, consider exactly how it will finish. Otherwise, unused goroutines may leak idly servicing an unattended channel.

  • C9. Keep Dave Cheney's Four Channel Axioms in mind:

    • A1. A send to a nil channel blocks forever.
    • A2. A receive from a nil channel blocks forever. (Why? Here is why. Example.)
    • A3. A send to a closed channel panics.
    • A4. A receive from a closed channel returns the zero value immediately.
  • C10. 'select' never selects a blocking case.

Goroutine Hints

See also:


转自:http://udhos.github.io/golang-concurrency-tricks/?utm_source=golangweekly&utm_medium=email

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值