map可以並發讀,不能並發寫


不能並發寫的原因

  1. 元素丟失 // TODO
  2. 破壞map的結構 // TODO

sync.Map相比RWLock在哪些點減少了加鎖的粒度,應該就是上述2點 // TODO

sync.Map的原理應該就是減小了鎖的粒度, 如果寫操作既不會造成元素丟失,也不會破壞map結構,就不需要加鎖,這就可以減小鎖的粒度。
sync.Map的原理不是讀寫分離嗎??? // TODO

map並發寫會報panic或fatal(調用panic包的throw函數)

panic可以被recover捕獲,但是fatal卻不能被捕獲,只能修改程序。

  • 如果使用的第三方庫有fatal問題,只能讓庫的作者修改或者自己修改后使用該庫。
    附錄1
I think JimB is the best answer here. If you have an external library throwing this error, you need to
A - Send a PR against that library to fix it. B - Create your own version of the library with no errors.
I strongly suggest that, before you integrate any external libraries in your code, you test it with Unit tests and Benchmarks to check if it is a deployable lib.

參考

  1. how to catch stack overflow error in golang。stack overflow就是一種fatal,不能被recover捕獲


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM