This is a creation in Article, where the information may have evolved or changed.
error: concurrent map writes
Have you ever met this error?
Origin of the problem
Today, I encountered a problem in the coding concurrency test process that directly kills the entire process. We know that as long as there is a goroutine happening in the Golang panic the whole process is hung up. At that time, a face Meng than. Begins checking stack information.
Problem analysis
At first I looked at the relevant solution. Mostly multithreaded operation map data structure must be locked. Otherwise this error must be present. I look at my code, I think I wrote the map structure has been added lock, additional lock mode:
- Universal Lock
typestruct { map[string]string Lock sync.Mutex}funcstringstring{ d.Lock.Lock() defer d.Lock.UnLock() return d.Data[k]}funcstring) { d.Lock.Lock() defer d.Lock.UnLock() d.Data[k]=v}
- Read/write Lock
typestruct { map[string]string Lock sync.RwMutex}funcstringstring{ d.Lock.RLock() defer d.Lock.RUnlock() return d.Data[k]}funcstring) { d.Lock.Lock() defer d.Lock.UnLock() d.Data[k]=v}
The above is a simple example of a concurrent operation map. So that terrible results would not have happened.
SB.
Before I said that the map is no problem, then why I met this problem. That's because my own HTTP framework gives big bugs, and multiple threads operate on the same action instance. Ggggg.
If you are experiencing this problem, check your map for multi-threading at once
This article source: Golang map data structure can not be read and write problems