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.

On Tuesday, August 2, 2022 at 8:21:26 PM UTC-7 kra...@skepticism.us wrote:

> 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.
>
> -- 
> Kurtis Rader
> Caretaker of the exceptional canines Junior and Hank
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3dcec585-86b0-4083-b869-2c533d7b2d65n%40googlegroups.com.

Reply via email to