如果我正确使用通道,我是否需要使用互斥体?
If I am using channels properly should I need to use mutexes?
如果我正确使用通道,是否需要使用互斥锁来防止并发访问?
如果正确使用通道,则不需要互斥锁。在某些情况下,使用互斥锁的解决方案可能更简单。
只需确保在多个 goroutine 尝试访问通道变量之前正确初始化保存通道值的变量。完成此操作后,访问通道(例如,向通道发送值或从通道接收值)在设计上是安全的。
带参考文献的支持文档(强调由我添加):
A single channel may be used in send statements, receive operations, and calls to the built-in functions cap
and len
by any number of goroutines without further synchronization. Channels act as first-in-first-out queues. For example, if one goroutine sends values on a channel and a second goroutine receives them, the values are received in the order sent.
Effective Go: Concurrency: Share by communicating
Concurrent programming in many environments is made difficult by the subtleties required to implement correct access to shared variables. Go encourages a different approach in which shared values are passed around on channels and, in fact, never actively shared by separate threads of execution. Only one goroutine has access to the value at any given time. Data races cannot occur, by design. To encourage this way of thinking we have reduced it to a slogan:
Do not communicate by sharing memory; instead, share memory by communicating.
This approach can be taken too far. Reference counts may be best done by putting a mutex around an integer variable, for instance. But as a high-level approach, using channels to control access makes it easier to write clear, correct programs.
这篇文章也很有帮助:The Go Memory Model
还引用了 sync
的包文档:
Package sync provides basic synchronization primitives such as mutual exclusion locks. Other than the Once and WaitGroup types, most are intended for use by low-level library routines. Higher-level synchronization is better done via channels and communication.
如果我正确使用通道,是否需要使用互斥锁来防止并发访问?
如果正确使用通道,则不需要互斥锁。在某些情况下,使用互斥锁的解决方案可能更简单。
只需确保在多个 goroutine 尝试访问通道变量之前正确初始化保存通道值的变量。完成此操作后,访问通道(例如,向通道发送值或从通道接收值)在设计上是安全的。
带参考文献的支持文档(强调由我添加):
A single channel may be used in send statements, receive operations, and calls to the built-in functions
cap
andlen
by any number of goroutines without further synchronization. Channels act as first-in-first-out queues. For example, if one goroutine sends values on a channel and a second goroutine receives them, the values are received in the order sent.
Effective Go: Concurrency: Share by communicating
Concurrent programming in many environments is made difficult by the subtleties required to implement correct access to shared variables. Go encourages a different approach in which shared values are passed around on channels and, in fact, never actively shared by separate threads of execution. Only one goroutine has access to the value at any given time. Data races cannot occur, by design. To encourage this way of thinking we have reduced it to a slogan:
Do not communicate by sharing memory; instead, share memory by communicating.
This approach can be taken too far. Reference counts may be best done by putting a mutex around an integer variable, for instance. But as a high-level approach, using channels to control access makes it easier to write clear, correct programs.
这篇文章也很有帮助:The Go Memory Model
还引用了 sync
的包文档:
Package sync provides basic synchronization primitives such as mutual exclusion locks. Other than the Once and WaitGroup types, most are intended for use by low-level library routines. Higher-level synchronization is better done via channels and communication.