In Go, can we synchronize each key of a map using a lock per key?

家住魔仙堡 提交于 2020-01-03 04:10:24

问题


In Go, can we synchronize each key of a map using a lock per key? Is map level global lock always required? The documentation says that any access to map is not thread safe. But if a key exists, then can it be locked individually?


回答1:


Not exactly, but if you are only reading pointers off a map and modifying the referents, then you aren't modifying the map itself.




回答2:


This is a simple implementation of what you want: mapmutex.

Basically, a mutex is used to guard the map and each item in the map is used like a 'lock'.



来源:https://stackoverflow.com/questions/40560007/in-go-can-we-synchronize-each-key-of-a-map-using-a-lock-per-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!