When a map is passed as an argument to a function running in a go routine and then passed to a function in another go routine, are they the same map? Like will modifying one affect the other? Because in my program, there is a map being passed down many times recursively but I don't think it should be being written do concurrently. Is there a way to ensure that I'm passing down a copy of the map?
#Concurrent map write
16 messages · Page 1 of 1 (latest)
yes they’re the same map
RIP 💀 . So each time I call a function with that map, should I initialize a new variable and pass that instead?
let’s take a couple steps back, what are you trying to accomplish with this
so you see, the program starts off by getting a list of rooms it is currently in, it adds in in a map of type map[string]bool so it doesn't have to go through an entire slice to see if it's in a room. after it adds it to that map, it calls another function in a go routine (using the map that it created) that scans that room for mentions of more rooms, which it then passes into the same function again in another go routine (after checking that map to see what rooms it is in), using that same map, which it modified in that function. It will just do this until it runs out of mentions of rooms. So like a web crawler basically.
sorry forgot to do that as a reply xD
ah yeah, you’re not going to want to copy that map as it’s going to not be too useful if each goroutine is operating on its own copy
mutex?
also yes it is kinda flawed ik xD
yep, you’d wrap the map in a mutex of some sort
alr bet
it says that a mutex must not be copied, would passing a pointer to it in the recursive function be good?
yep that would work