并发访问映射 [英] Map with concurrent access

查看:38
本文介绍了并发访问映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在并发访问的程序中使用映射时,是否需要在函数中使用互斥锁来读取值?

When you use a map in a program with concurrent access, is there any need to use a mutex in functions to read values?

推荐答案

多个读者,没有作者也可以:

Multiple readers, no writers is okay:

https://groups.google.com/d/msg/golang-nuts/HpLWnGTp-n8/hyUYmnWJqiQJ

一个作家,没有读者是可以的.(否则地图就不太好用了.)

One writer, no readers is okay. (Maps wouldn't be much good otherwise.)

否则,如果至少有一个写入者和至少一个写入者或读取者,则所有读取者写入者必须使用同步来访问地图.互斥锁适用于此.

Otherwise, if there is at least one writer and at least one more either writer or reader, then all readers and writers must use synchronization to access the map. A mutex works fine for this.

这篇关于并发访问映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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