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捕获
posted @ 2020-04-03 23:55  sicnu-yudidi  阅读(1042)  评论(0编辑  收藏  举报