英文:
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")
。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论