what would happen if runtime does not throw("concurrent map writes") but panic("concurrent map writes")

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

what would happen if runtime does not throw("concurrent map writes") but panic("concurrent map writes")

问题

根据icza的说法:

> 如果你让你的应用程序保持这样的状态而不崩溃,你可能会在运行时遇到神秘的、未定义的行为。

如果在出现“concurrent map writes”时,一个goroutine无法写入map,那么可以考虑使用panic("concurrent map writes")而不是throw("concurrent map writes")

英文:

https://stackoverflow.com/questions/39288741/how-to-recover-from-concurrent-map-writes/39289246#39289246?newreg=02bd7b743b044ed0bbecdd49b84dc89a

as icza said:

> If you would leave your app like that and it wouldn't crash, you could
> experience mysterious, undefined behavior at runtime.

If one goroutine fails to write to a map in case of "concurrent map writes", how about just panic("concurrent map writes") instead of throw("concurrent map writes").

答案1

得分: 5

运行时无法检测到竞争条件,但它可以检测到当map的内部状态被数据竞争破坏时,会发生什么情况,并且这就是throw("concurrent map writes")throw("concurrent map read and map write")的用途(在运行时广泛使用throw,当没有安全的方式继续执行程序时,用于中止程序)。在这里使用panic会暗示你可以从这种情况中恢复,但实际上是无法恢复的,因为已经知道程序状态已被破坏。

英文:

The runtime cannot detect races, but it can detect when the internal state of a map has been corrupted by a data race, and that is what throw("concurrent map writes") and throw("concurrent map read and map write") are for (throw is used extensively in the runtime, to abort the program when there is no safe way to proceed). Using a panic here would imply that you can recover from this situation, but there is no way to recover, because it is already known that the program state has been corrupted.

huangapple
  • 本文由 发表于 2021年6月21日 19:42:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/68067330.html
匿名

发表评论

匿名网友

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

确定