问题
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