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:
- [go] solve the fatal error of go: concurrent map writes map non concurrent security
- Error handling when ABP specifies map object during map operation
- Android 9 (P) recovery upgrade Map of ‘@/cache/recovery/block.map’ failed problem analysis guide
- Map to vector pair map.second sort
- [Vue warn]: Error in nextTick: “TypeError: Cannot read property ‘map‘ of null“
- #An error is reported by the chart map component of renfast framework
- VS 2010: An error occurred loading a configuration file: Failed to map the path ‘/’.
- Python read / write file error valueerror: I/O operation on closed file
- How to get all the keys of map by golang
- Common problems of shadow map in OpenGL
- Vue install reports an error operation not allowed
- Consult IDE log for more details (Help | Show Log),read failed, socket might closed or timeout,
- Struts 2 encapsulates form data into list and map sets
- Common attributes and methods of list and map in Dar
- BUG: Bad page map in process XXX pte:800000036fae6227 pmd:35be8c067
- PHP function file_ get_ Contents() reports an error when using HTTPS protocol: SSL operation failed
- JSON and map convert to each other (using fastjson)
- OpenGL cube texture map
- Transformation of JS map and JSON
- chrome net_error_map