Easynvr operation log reports an error. Fatal error: concurrent map read and map write troubleshooting

As we all know, most of our products are compiled in go language. In the case of concurrency, read-only is thread safe, and read-write is thread unsafe.

Recently, in the easynvr site of a project, we checked the log and found the error message: fatal error: concurrent map read and map write. The error message shows that there are concurrent map reads and writes, that is, two concurrent functions are used to read and write the map continuously, resulting in race problems.

Find the code and find that concurrent read/write is used in the code:

The code needs to be modified here. Below, we replace the built-in map type with the concurrency safe sync.map.

Write reference codes concurrently as follows:

The concurrent read reference code is as follows:

Add: for concurrency problems that are not easy to find, you can use the – race parameter for concurrency detection:

func main() {
	a := 1
	go func() {
		a = 2
	}()
	a = 3
	fmt.Println(a)

	time.Sleep(time.Second * 1)
}

Read More: