I know that maps aren't safe for concurrent access, but I've always thought this meant only that you can't add or delete keys concurrently.
Let's say I already have a map[string]int that has been filled with keys that are file names. I won't be adding any more keys. I want to add an int value for each existing key to contain the file size. Could I do this by running a bunch of go routines, one per file name key? (Let's ignore for now whether doing this would be any faster than not using go routines.)
The Go Language spec doesn't directly define the rules for concurrent memory safety. Instead, those rules are explicitly defined in the Go Memory Model (https://go.dev/ref/mem). I looked there but I wasn't able to find what I was looking for. The Go FAQ has a section on Atomic Maps (https://go.dev/doc/faq#atomic_maps) says
"As long as all goroutines are only reading—looking up elements in the map, including iterating through it using a for range loop—and not changing the map by assigning to elements or doing deletions, it is safe for them to access the map concurrently without synchronization."
The question here is what is meant by "assigning to elements".
My thinking is that this should be allowed because adding a file name to the map should have also added space for an int value. So, I'd just be modifying this int, and not changing the hash table data structure. Is this correct?
Cordially,
Jon Forrest
On Jul 28, 2026, at 11:15 AM, 'jlfo...@berkeley.edu' via golang-nuts <golan...@googlegroups.com> wrote:
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/873345e3-4dc6-4578-a408-b356f3ab04abn%40googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/533BF668-A19F-47E7-BDA0-4898A7DBBA03%40me.com.
On Jul 29, 2026, at 5:00 AM, Haddock <bienle...@gmail.com> wrote:
Forgot to mention: The current implementation of ConcurrentHashMap in the more recent JDKs is quite optimized and it is meanwhile hard to see what it does. If you take an older JDK (JDK8 or upwards, not sure ConcurrentHashMap already exists in JDK8) you see an older implementation with an array of 16 segments and some algorithm that from the key derives a number between 0 and 15. That approach is easy to implement in some other language.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/499f95fa-a71f-4333-a32b-5cc91b4f4975n%40googlegroups.com.