在Go中,我们可以使用每个键的锁来同步地图的每个键吗? [英] In Go, can we synchronize each key of a map using a lock per key?

查看:121
本文介绍了在Go中,我们可以使用每个键的锁来同步地图的每个键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Go中,我们可以使用每个键的锁来同步地图的每个键吗?
总是需要地图级全局锁吗?
文档说任何对map的访问都不是线程安全的。但是,如果一个键存在,那么它可以单独锁定?

解决方案

不完全是,但如果你只是读指针映射和修改参照物,那么你不会修改地图本身。


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?

解决方案

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

这篇关于在Go中,我们可以使用每个键的锁来同步地图的每个键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆