Map is not concurrency safe, when there are multiple concurrent growths reading and writing the same map
A panic error occurs
concurrent map writes
For example, this error occurs in the following code:
var mMap map[int]int
func TestMyMap(t *testing.T) {
mMap = make(map[int]int)
for i := 0; i < 5000; i++ {
go func() {
mMap[i] = i
}()
go readMap(i)
}
}
func readMap(i int) int {
return mMap[i]
}
There are many ways to solve this problem. Now we use read-write lock,
Concurrent access to map is not safe, and undefined behavior will appear, leading to program exit. Therefore, if you want to access the map concurrently in multiple coroutines, you must provide some synchronization mechanism. Generally, you can control the concurrent access to the map by reading and writing the lock sync.rwmutex. Encapsulating the map and sync.rwmutex can realize the secure concurrent access to the map
Code after transformation
type SMap struct {
sync.RWMutex
Map map[int]int
}
func (l *SMap) readMap(key int) (int, bool) {
l.RLock()
value, ok := l.Map[key]
l.RUnlock()
return value, ok
}
func (l *SMap) writeMap(key int, value int) {
l.Lock()
l.Map[key] = value
l.Unlock()
}
var mMap *SMap
func TestMyMap(t *testing.T) {
mMap = &SMap{
Map: make(map[int]int),
}
for i := 0; i < 5000; i++ {
go func() {
mMap.writeMap(i, i)
}()
go readMap(i)
}
}
func readMap(i int) (int, bool) {
return mMap.readMap(i)
}
There are three ways:
1. Use channel
2. Use sync. Map
3. Use map but lock it
Read More:
- Easynvr operation log reports an error. Fatal error: concurrent map read and map write troubleshooting
- Map to vector pair map.second sort
- 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
- How to get all the keys of map by golang
- Go run error go run: cannot run non main package
- chrome net_error_map
- Quickly convert map to ordered array
- BUG: Bad page map in process XXX pte:800000036fae6227 pmd:35be8c067
- JSON and map convert to each other (using fastjson)
- Lambda set to map duplicate key error solution
- Common problems of shadow map in OpenGL
- OpenGL cube texture map
- Transformation of JS map and JSON
- Common attributes and methods of list and map in Dar
- Struts 2 encapsulates form data into list and map sets
- NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
- Connection authorization failure occurred. Reason: local security service non retryable error solution
- VS 2010: An error occurred loading a configuration file: Failed to map the path ‘/’.
- WordPress website map sitemap.xml error repair