Concurrent Maps

xuanbao · · 697 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, im very new to golang so please pardon my unawareness.... I&#39;m writing a procedure that uses goroutines to concurrently iterate through a large population of nodes (points on a map). I&#39;d like to store the nodes in a hashmap and be able to concurrently read from the hashmap while iterating through them in the goroutines.</p> <p>I&#39;ve noticed several people in forums saying that it&#39;s &#34;misguided&#34; to try and use a concurrent map or concurrent set.... and that golang was not designed to be implemented this way.</p> <p>My question is, How am I supposed to implement a concurrent read operation without a concurrent-map? (and without making multiple copies of the map)... have the forums lead me astray?</p> <p>thank you in advance for any light you can shed</p> <hr/>**评论:**<br/><br/>bradfitz: <pre><p>Go maps are safe for concurrent reads.</p> <p>You just can&#39;t concurrently read+write or write+write.</p> <p>Once you need to, use a Mutex or RWMutex, etc.</p></pre>vruin: <pre><p>Adding to the write scenario, to avoid contention, although <strong>not advised</strong>, you can use atomic <a href="https://golang.org/pkg/sync/atomic/#example_Value_readMostly" rel="nofollow">as explained in this example</a>. Or trust the untested code generator from a random internet guy (me): <a href="https://github.com/ninibe/atomicmapper" rel="nofollow">https://github.com/ninibe/atomicmapper</a></p></pre>OccamsMirror: <pre><blockquote> <p>Once you need to, use a Mutex or RWMutex, etc.</p> </blockquote> <p>Always use an RWMutex, as a concurrent read+write will cause a panic. Much better to do RLock when doing a read.</p></pre>dnaeon: <pre><p>Here&#39;s how you can implement your own concurrent map and slice types.</p> <p><a href="http://dnaeon.github.io/concurrent-maps-and-slices-in-go/" rel="nofollow">http://dnaeon.github.io/concurrent-maps-and-slices-in-go/</a></p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

697 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传