Hi Brian, thanks for replying!

So you mean that I have to use 5 Mutexes for Market data of each one source
? That is exactly what I want to do. Every action of RW marketMap[i] must
comes in one line, but RW marketMap[j] concurrently is allowed. And mapLocks
now is fine in golang?

The reason why I do not use channel is that not each one market updates is
needed, but I need to take samples from time to time.

On Wed, Feb 23, 2022 at 6:06 PM Brian Candler <b.cand...@pobox.com> wrote:

> On Wednesday, 23 February 2022 at 08:49:26 UTC yan.z...@gmail.com wrote:
>
>> I have heard that sync.Map allows for reading simultaneously among
>> multiple goroutines.
>>
>
> It does.  But it doesn't synchronize what you do with the values returned
> from the map.  So if you retrieve a *copy* of a value, or replace the
> value, it's fine.  But if you retrieve a pointer, and then either read via
> that pointer or update the object via that pointer, you'll be back to a
> race condition.  You should note that various Go data types include
> pointers internally, especially slices.  However in your case, you're
> proposing that a sync.Map (safe) contains pointers to *Market data (unsafe
> to access concurrently).  So you would still need a mutex to protect each
> Market object, and every read and write would have to apply that mutex.
>
> Personally, I would avoid locks and use channels instead.  It's much
> easier to reason about.  Have one goroutine per market, and have it accept
> 'update' and 'read' messages over a channel (or two different channels).
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/6I_kS6Pya0M/unsubscribe.
> To unsubscribe from this group and all its topics, 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/1f5beaae-e5d0-4e13-bbe1-28d28ee47e9dn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/1f5beaae-e5d0-4e13-bbe1-28d28ee47e9dn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CADEX6_VXBtWuVCKCZziejjhEf8PWmEjzQhmW4NyAZkXdqJGWJw%40mail.gmail.com.

Reply via email to