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

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

问题描述

当您在并发访问的程序中使用地图时,是否需要在函数中使用互斥体来读取值?

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天全站免登陆