Map is not concurrently safe, when there are multiple concurrent groutines reading and writing the same map
Panic error will occur
concurrent map writes
For example, the following code will cause this error:
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 error. Now we take the way of read-write lock.
Concurrent access to the map is not safe, and undefined behavior will occur, causing the program to exit. Therefore, if you want to access the map concurrently in a multi-coroutine, you must provide a synchronization mechanism. Generally, the concurrent access control to the map is achieved through the read-write lock sync.RWMutex, and the map and sync.RWMutex can be encapsulated to achieve the map Secure concurrent access
Transformed code
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 must be locked
Read More:
- Golang Error: fatal error: concurrent map read and map writ
- [Solved] Golang Error: fatal error: concurrent map writes
- How to Solve Go Error: concurrent map iteration and map write
- Hash_map is deprecated and will be REMOVED. Please use unordered_map.
- How to Solve Error: IncompleteElementException: Could not find result map…
- How to Solve Cordova integrates Gaode Map key error
- Vue Import Baidu map error: BMap is not defined, eslint BMap reports an error
- How to Solve Error: Missing type map configuration or unsupported mapping
- Max virtual memory areas vm.max_map_count [65530] is too low, increase to at least
- [Solved] cocopod Error: failed: undefined method `map‘ for nil:NilClass
- [Solved] IP Core Map Compile Error: Buffers of the same direction cannot beplaced in series.
- [Solved] Searching for inspections failed: undefined method `map‘ for nil:NilClass“
- Failed to remove multipath map 320b508ca45022b80 [How to Solve]
- [Solved] Eclipse Connect Hadoop error: /An internal error occurred during: “Map/Reduce location status updater“
- How to Solve Spark Writes Hudi Error
- How to Solve spark Writes Files to odps Error
- [Solved] Hystrix error: java.util.concurrent.TimeoutException: null
- How to Solve Error: “initializer element is not constant”
- Error: env [NODE_ENV] is not set (How to Solve)
- Kafka Creates and Writes Logs Error [How to Solve]