U
U
UntitledNikname2020-11-25 23:32:53
go
UntitledNikname, 2020-11-25 23:32:53

How to work with concurrent access to one memory cell?

Small chat (for self-study) on GO. The "server" package has "map[string]net.Conn" where all active channels are stored.
I delete the empty channel from memory as soon as the last user exits.
During testing, I noticed that there are 2 different streams .. one is trying to delete an empty channel and the second is trying to write a new connection there.
It's not clear how to work with this.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Samsonov, 2020-11-25
@UntitledNikname

Use mutexes when running concurrently with map
https://gobyexample.com/mutexes

K
Klars, 2020-11-27
@Klars

Mutexes
A visual graph with the code https://golangbot.com/mutex/
+ it is mandatory for readers and writers to use RLock and RUnlock (lock on reading the value or on writing)
very useful

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question