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

时间:2016-11-12 05:54:46

标签: go concurrency mutex

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?

2 个答案:

答案 0 :(得分:0)

不完全是,但如果你只是从地图上读指针并修改指示物,那么你就不会修改地图本身。

答案 1 :(得分:0)

这是您想要的简单实现:mapmutex

基本上,互斥锁用于保护地图,地图中的每个项目都像“锁定”一样使用。