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'.