Re: [go-nuts] concurrent read/write different keys in map

2022-08-04 Thread 'Keith Randall' via 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.

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  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.


Re: [go-nuts] concurrent read/write different keys in map

2022-08-02 Thread Kurtis Rader
On Tue, Aug 2, 2022 at 7:59 PM burak serdar  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/CABx2%3DD-w7V1%2BC%3DO3W7JVzJQtcHF3C5pO5GtJvhO%3Dd6hg_ZRu6g%40mail.gmail.com.


Re: [go-nuts] concurrent read/write different keys in map

2022-08-02 Thread burak serdar
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.



On Tue, Aug 2, 2022 at 7:31 PM ag9920  wrote:

> 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?
>
> --
> 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/76788485-6809-4397-bf94-536a0d0bf0b3n%40googlegroups.com
> 
> .
>

-- 
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/CAMV2Rqq50NX8WGGkPK%2BhRSz91QpUHzWWhMmz1c8RNKpa9dncZg%40mail.gmail.com.


Re: [go-nuts] concurrent read/write different keys in map

2022-08-02 Thread Kurtis Rader
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.

On Tue, Aug 2, 2022 at 6:31 PM ag9920  wrote:

> 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?
>
> --
> 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/76788485-6809-4397-bf94-536a0d0bf0b3n%40googlegroups.com
> 
> .
>


-- 
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/CABx2%3DD8ZsUO71KjB4n4o8MZzraO%2B5R-RHoHGMJpqyykcaxY7gg%40mail.gmail.com.


[go-nuts] concurrent read/write different keys in map

2022-08-02 Thread ag9920
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?

-- 
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/76788485-6809-4397-bf94-536a0d0bf0b3n%40googlegroups.com.