2018-08-04 17:38
浏览 40

Golang RWMutex在地图内容上的编辑

I'm starting to use RWMutex in my Go project with map since now I have more than one routine running at the same time and while making all of the changes for that a doubt came to my mind.

The thing is that I know that we must use RLock when only reading to allow other routines to do the same task and Lock when writing to full-block the map. But what are we supposed to do when editing a previously created element in the map?

For example... Let's say I have a map[int]string where I do Lock, put inside "hello " and then Unlock. What if I want to add "world" to it? Should I do Lock or can I do RLock?

图片转代码服务由CSDN问答提供 功能建议

我开始在我的Go项目中将 RWMutex </ code>与 map < / code>至今,我有多个例程同时运行,并且在进行所有更改时都想到了一个疑问。</ p>

我知道 我们仅在读取时允许使用 RLock </ code>来允许其他例程执行相同的任务,而在写入以完全阻止地图时则必须使用 Lock </ code>。 但是在编辑地图中先前创建的元素时我们应该怎么做?</ p>

例如...假设我有一个 map [int] string </ code> 在我执行 Lock </ code>的位置,将其放入“ hello” </ code>内,然后 Unlock </ code>。 如果要向其中添加“世界” </ code>,该怎么办? 我应该执行 Lock </ code>还是执行 RLock </ code>?</ p> </ div>

2条回答 默认 最新

相关推荐 更多相似问题