[Solved] Why is this function not thread safe in golang?


I haven’t analyzed all of it, but definitely the modification of mapStore from multiple goroutines is unsafe:

mapStore[*res.someData] = append(mapStore[*res.someData], res)

But as a starting point, run this under the race detector. It’ll find many problems for you.

This is also clearly unsafe:

resSlice := append(resSlice, res)

But it also doesn’t quite do what you think. This creates a new local variable called resSlice that shadows the outer one, but it also modifies the outer one (see comments below). Beyond the fact that two things may try to append at the same time and collide, append can move the whole slice in memory if it needs to reallocate, so this can cause thread-safety issues even if you put locks around it.

Typically, rather than having each goroutine update some central variable, you want to have each goroutine pass its results back on a channel. Then have the main function collect all the values and update the variables. See Go Concurrency Patterns: Pipelines and cancellation for some examples.

4

solved Why is this function not thread safe in golang?