Concurrency Patterns in Go

Wu_XMing發表於2018-12-20

Concurrency in practice

  • Avoid blocking,avoid race conditions
  • Use channels to avoid shared state. Use select to manage channels.
  • where channels don't work:
    • try to use tools from the sync package first
    • in simple cases or when really needed:try lockless code

Guidelines for non-blocking code

  • Don't switch between atomic and non-atomic functions
  • Target and exploit situations which enforce uniqueness
  • Avoid changing two things at a time
    • Sometimes you can exploit bit operations
    • Sometimes intelligent ordering can do the trick
    • Sometimes it's just not possible at all

相關文章