TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Are Go maps sensitive to data races?

43 pointsby spaceyover 9 years ago

8 comments

alkonautover 9 years ago
Any data structure that isn&#x27;t explicitly designed for concurrent access will not work when used concurrently.<p>Since a concurrent map (aka. Dictionary aka. Hashtable) is a lot more complex and slow than a non concurrent one, it&#x27;s very very unlikely that Go would ship with its standard map type being a concurrent one. It would be a huge waste.<p>More likely there is a separate concurrent map type. It&#x27;s exactly the same in other languages with mutable collections (Java, .NET, C++, ...).
评论 #10688925 未加载
JulianMorrisonover 9 years ago
The other reason: things <i>inside</i> maps are often pointer types with mutable data. Even if access to the map is properly locked, if two threads take a pointer out of it and directly or indirectly keep a reference (for example, re-slicing a slice without copying it), then one can stomp on the data the other is reading.<p>The fixes for this are subtle and annoying. Either you have to lock around your data, make it somehow immutable, or force copying rather than referencing (by making it &quot;value&quot; data).<p>Go would really benefit from a port of Clojure&#x27;s data structures.
kasey_junkover 9 years ago
A concurrent map is the top of my wants list for go by a long stretch. The next thing on that list is a high performance concurrent queue. That these things aren&#x27;t available is a direct result of the decision to not support user defined generics in go.
评论 #10688917 未加载
zalmoxesover 9 years ago
Mentioned in the FAQ: <a href="https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;faq#atomic_maps" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;faq#atomic_maps</a>
grabcocqueover 9 years ago
&quot;no, there is nothing wrong with Go’s map implementation.&quot;<p>&quot;Getting your locking wrong will corrupt the internal structure of the map.&quot;<p>Ahem.<p>If you have a non-concurrency-safe map implementation in a language that&#x27;s advertised as being good for concurrency, there is <i>definitely</i> something wrong.
评论 #10688770 未加载
评论 #10688919 未加载
评论 #10688876 未加载
评论 #10688782 未加载
khgvljhkbover 9 years ago
Golang devs should have a look at Clojure and it&#x27;s `core.async` library. It works very similarly to Go (heavily inspired by the good stuff), but all data structures are persistent, meaning you will never have problems with concurrent mutations (because there there are no mutations in the API, only internally).<p>I recommend Go users to install the boot utility, and playing around with Clojure &amp; core.async. These communities can share many things.
评论 #10689625 未加载
SixSigmaover 9 years ago
Share memory by communicating; don&#x27;t communicate by sharing memory.<p><a href="https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;codewalk&#x2F;sharemem&#x2F;" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;codewalk&#x2F;sharemem&#x2F;</a>
评论 #10689084 未加载
vinceyuanover 9 years ago
&gt; <i>Go maps are not goroutine safe</i><p>Which types in Go are goroutine safe? Do we need to use sync.Mutex for all global variables which are read and written in goroutines? My understanding is only read-only variables are goroutine safe.
评论 #10689073 未加载