<p>So I'm aware of both <a href="https://blog.golang.org/go-maps-in-action" rel="nofollow">this</a> and <a href="https://golang.org/doc/faq#atomic_maps" rel="nofollow">that</a> saying maps are not safe for concurrent use.</p>
<p>My question is,</p>
<blockquote>
<p>What (if any) map operations are guaranteed when accessed concurrently?</p>
</blockquote>
<p>For example:</p>
<ul>
<li>Concurrent reads without concurrent writes?</li>
<li>Concurrent read/writes of keys already existing in the map of values which are primitive types (int/float/pointer/etc)?</li>
<li>Concurrent reads of keys with concurrent writes to other keys not being read?</li>
</ul>
<p>It'd be nice to know if it's possible to safely use a sub-set of the features of maps concurrently. Thanks!</p>
<p>EDIT: Thanks folks! <a href="/u/dgryski" rel="nofollow">/u/dgryski</a> pointed out that Go 1.6 will panic if detects concurrent read/write on a map, so I think that answers the question (unless there's some nuance they missed).</p>
<hr/>**评论:**<br/><br/>dgryski: <pre><p>Only reads. Go 1.6 will panic if it detects any map write executing concurrent to any map read.</p></pre>dlsniper: <pre><p>You can safely do concurrent reads from the map as long as you don't write to it. If you want to write to it then you have to grt exclusive access to the whole map (no other goroutine should perform any operation on it at the same time) Use a rwmutex to flag access to it. </p></pre>Akkifokkusu: <pre><p>Just to clarify what might be the premise of one of your questions:</p>
<blockquote>
<p>Concurrent read/writes of keys already existing in the map of values which are primitive types (int/float/pointer/etc)?</p>
</blockquote>
<p>"Primitive types" have the same concurrency guarantee as maps. Concurrent reads are safe. Writes concurrent with reads or other writes are not.</p></pre>earthboundkid: <pre><p>Adding to that: if you need atomic updates to an int and don't want to use channels or mutexes for some reason, there's a sync/atomic package in the standard library. </p></pre>joushou: <pre><p>Remember that sync/atomic is low-level, and can break in unexpected ways if you don't know what you're doing. Atomic access to an int64 on some 32bit platforms has certain alignment requirements, for example.</p></pre>BenjaminZarzycki: <pre><blockquote>
<p>Writes concurrent with reads or other writes are not [safe]</p>
</blockquote>
<p>How are concurrent read/writes on an int32 unsafe? isn't it guaranteed to be read as either the value before the write or the value after the write?</p></pre>Akkifokkusu: <pre><p>Is not knowing which value will be read considered safe? Consider incrementing a counter. That involves a two-step process, a read followed by a write. But without a mutex or the functions in <code>sync/atomic</code>, there's no guarantee that nothing else will modify the variable between the two steps.</p>
<p>You might be interested in <a href="https://golang.org/ref/mem" rel="nofollow">https://golang.org/ref/mem</a>, especially the "Advice" and "Incorrect synchronization" sections.</p></pre>DualRearWheels: <pre><p>Just to add that RWMutex is made for these situations <a href="https://golang.org/pkg/sync/" rel="nofollow">https://golang.org/pkg/sync/</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传