thread safety of map when each goroutine access a different key

123 views
Skip to first unread message

Albert Tedja

unread,
Mar 13, 2017, 2:39:59 AM3/13/17
to golang-nuts
Hello,

I know map itself isn't threadsafe, but I am just wondering how threadsafe it is if you can be absolutely sure that each goroutine accesses a different key in the map.
Say, goroutine 1 accesses mymap["1"]
and goroutine 2 accesses mymap["2"] and so on.

Thank you in advance.

Konstantin Khomoutov

unread,
Mar 13, 2017, 3:20:42 AM3/13/17
to Albert Tedja, golang-nuts
Contrary to a struct type, values of which are just pretty "static"
pieces of memory, where the compiler known an offset (in bytes) of each
field relative to the value's starting address, a value of a map type
contains a pointer to an intricate *dynamic* thing, where each insertion
of a new value might lead to drastic changes like moving of unrelated
values already stored in that map in memory.

Because of this, the only thing guaranteed to work concurrently with Go
maps is reading them. So if your goroutites only read a map value,
then it does not matter which values they read. If they write to a map
(that is, insert into it or delete from it), you must synchronize the
accesses to that no write even happens concurrently with either another
write or a read.

On the other hand, consider that if you store in the map pointers to
actual values which are themselves not stored in a map, then, after
reading such a pointer from a map, you're free to deal with the value
it points to concurrently with map modification. That's because in
this scenario, the only values stored by the map are pointers, and
hence insertion to the map and deletions from it might move those
pointers in memory but not the values they point to.

Albert Tedja

unread,
Mar 13, 2017, 6:03:16 AM3/13/17
to golang-nuts, nicho...@gmail.com
Great. Thanks! Looks like it's just better if I return a struct if these goroutines are operating independently anyway.

Haddock

unread,
Mar 13, 2017, 6:58:15 AM3/13/17
to golang-nuts
You can download Java (JDK5 or later) and have a look at class ConcurrentHashMap. This will give you a blueprint about how this can be implemented using segments. After segment lookup for a given hash code only the segment needs to be locked, but not any more the entire map. Note, that you still have to lock the entire map when you do traversals. So it is recommend to avoid traversals where possible.

Haddock

unread,
Mar 13, 2017, 7:09:24 AM3/13/17
to golang-nuts
Forgot to mention that ConcurrentHashMap has 256 segments (if I remember right). So you can have 256 map operations going on concurrently.

Marvin Stenger

unread,
Mar 13, 2017, 10:47:22 AM3/13/17
to golang-nuts
Why do you even use a shared map, if you only use distinct keys per goroutine. There probably is a better pattern for your use case.


Am Montag, 13. März 2017 07:39:59 UTC+1 schrieb Albert Tedja:
Reply all
Reply to author
Forward
0 new messages