concurrent read/write different keys in map

1,868 views
Skip to first unread message

ag9920

unread,
Aug 2, 2022, 9:31:13 PM8/2/22
to golang-nuts
Hi! If I have several 5 goroutines read/write 5 different keys in map independently without a Mutex/RWMutex. Each goroutine just read/write their own corresponding key. No intersection.

In such a case, no goroutines will operate on the same key, does that mean it's safe?

Or maybe each goroutine just write to its corresponding key once, with no read, will that be safe?

Kurtis Rader

unread,
Aug 2, 2022, 9:50:36 PM8/2/22
to ag9920, golang-nuts
Go maps are only concurrent safe if all accesses are reads (lookups) after the map is initialized. So your scenario is not safe and can result in a panic as well as other undefined behavior. The Go standard library has a concurrent safe map implementation (see https://pkg.go.dev/sync#Map). There are also many third-party implementations that may be better suited for your use case. Alternatively, you can, of course, use a mutex to serialize access but it's probably safer to just use a concurrent safe implementation.

--
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 on the web visit https://groups.google.com/d/msgid/golang-nuts/76788485-6809-4397-bf94-536a0d0bf0b3n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

burak serdar

unread,
Aug 2, 2022, 10:59:50 PM8/2/22
to ag9920, golang-nuts
What exactly do you mean by "read/write 5 different keys"?

If you have a map[int]*SomeStruct, for instance, and if you initialize this map with some entries, and then if you have multiple goroutines all performing lookups of distinct keys and modifying the contents of *SomeStruct, it would be safe.

If you have multiple goroutines adding/removing keys from the map, that would not be safe.

It would be interesting to know if rewriting existing distinct keys from multiple goroutines would be safe or not.



--

Kurtis Rader

unread,
Aug 2, 2022, 11:21:26 PM8/2/22
to burak serdar, ag9920, golang-nuts
On Tue, Aug 2, 2022 at 7:59 PM burak serdar <bse...@computer.org> wrote:
What exactly do you mean by "read/write 5 different keys"?

If you have a map[int]*SomeStruct, for instance, and if you initialize this map with some entries, and then if you have multiple goroutines all performing lookups of distinct keys and modifying the contents of *SomeStruct, it would be safe.

If you have multiple goroutines adding/removing keys from the map, that would not be safe.

It would be interesting to know if rewriting existing distinct keys from multiple goroutines would be safe or not.

The O.P. question is slightly ambiguous but it seems like they were asking about a map initialized with five distinct keys whose values are then read/mutated by five goroutines. Each goroutine always reading/mutating the same key that is not read/mutated by any other goroutine. While that is probably safe to do given how a typical hashmap would be implemented it's my understanding the Go implementation makes no such guarantee. Also, if that is really the situation described by the O.P. I don't understand why you would even use a map. I suspect they did not accurately describe their scenario.

Keith Randall

unread,
Aug 4, 2022, 12:31:15 PM8/4/22
to golang-nuts
Updating existing keys in parallel is not guaranteed to be safe. If you want to modify values, you'd have to do the trick mentioned above where instead of a map[K]T you use a map[K]*T and to do modification you do *m[k] = T{...} or whatever.

Go maps do incremental growth work on every update, so even if you're just updating an existing entry, the map implementation may still be moving data around. Doing that with multiple goroutines simultaneously would be bad.

Reply all
Reply to author
Forward
0 new messages