Golang使并发更容易的关键原因

huangapple go评论76阅读模式
英文:

Key reasons for golang to make concurrency easier

问题

我的理解大致正确吗?

  • go 在编译时可以大部分检测到 死锁

  • go 可以使用 chan 来最小化 竞态条件,因为在任何特定的 chan 上,只有单个发送者或接收者 goroutine 可以同时访问。

英文:

Is my understanding roughly right below?

  • go can mostly detect dead lock at compile-time.

  • That go can use chan to minimize race condition is because only single sender or receiver goroutine can have access to any specific chan at a time.

答案1

得分: 3

我不会说那是准确的。关于死锁,编译时没有任何保证,如果你使用互斥锁不当,就会发生死锁,没有编译器可以防止这种情况。你可以很容易地测试竞态条件,但这是不同的问题。

关于第二点,通道会序列化你的异步操作,但我认为你的陈述并没有太多意义。一堆 goroutine 可以同时向通道写入和读取数据。它就像一个队列,用于存放数据,没有任何协调保证。如果多个例程同时从通道读取或写入数据,你不会因此而出现恐慌,但是如果你希望程序正常工作,你需要自己使用通道来协调这些例程。

英文:

I wouldn't say that's accurate. On the first point there aren't any compile time guarantees about dead locking, if you use a mutex poorly you will be dead locking, no compiler can prevent that. You can test for race conditions easily, but that's different.

On the second point, the channel serializes your asynchronous operations but I don't think how you state it makes much sense. A bunch of goroutines can be writing to and reading from it. It's just like a queue to put the data in, no coordination is guaranteed. You won't panic due to multiple routines reading off it or writing to it at the same time but if you have that happening Go isn't doing anything to make your program work well, you have to coordinate the routines yourself using channels.

答案2

得分: 2

不,第一个完全错误,第二个至少陈述不清楚或奇怪。

英文:

No, the first is completely wrong and the second is at least stated unclear or strange.

答案3

得分: -2

根据这个教程,它可以捕获一些死锁。虽然我还没有仔细阅读这个教程...

http://guzalexander.com/2013/12/06/golang-channels-tutorial.html

英文:

According to this tutorial, it can catch some deadlocks. I've not gone though this tutorial though...

http://guzalexander.com/2013/12/06/golang-channels-tutorial.html[enter link description here]1

huangapple
  • 本文由 发表于 2015年8月26日 01:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/32210655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定