Go provides a data structure called map that can be translated into maps that correspond to dictionaries and hash tables in other languages. With map, you can define a key and value, which you can then get, set, and delete from the map, especially for scenarios where data is found. However, the use of the map has a certain limit, if you are reading and writing map in a single process, then there is no problem, if multiple concurrent access to a map, may cause the program to exit, and print the following error message:
Fatal error:concurrent map read and map write
The above error is not encountered every time, and if the number of concurrent accesses is small, the likelihood is even smaller. For example, the following program:
1 Package Main2 3 Func Main () {4Map: =Make (map[int]int)5 6 forI: = 0; I < 10; i++ {7Go Writemap (Map, I,i)8Go Readmap (Map,i)9 }Ten One } A -Func Readmap (Map map[int]int,Keyint) int { - returnmap[Key] the } - -Func Writemap (Map map[int]int,KeyIntvalue int) { -map[Key] =value +}
Loop only 10 times, resulting in 20 concurrent access map, the program basically does not go wrong, but if the number of cycles, such as 100,000, run the following program basically every time error:
1 Package Main2 3 Func Main () {4Map: =Make (map[int]int)5 6 forI: = 0; I < 100000; i++ {7Go Writemap (Map, I,i)8Go Readmap (Map,i)9 }Ten One } A -Func Readmap (Map map[int]int,Keyint) int { - returnmap[Key] the } - -Func Writemap (Map map[int]int,KeyIntvalue int) { -map[Key] =value +}
The official Go Blog has the following instructions:
Maps is not safe for concurrent Use:it's not defined what happens when you read and write to them simultaneously. If you need to read from and write to a map from concurrently executing Goroutines, the accesses must is mediated by some Kind of synchronization mechanism. One common-to-protect maps are with sync. Rwmutex.
The Go FAQ is explained as follows:
After long discussion it is decided that the typical use of maps do not require safe access from multiple Goroutines, an D in those cases where it did, the map is probably part of some larger data structure or computation this was already SYN Chronized. Therefore requiring that all map operations grab a mutexes would slow down most programs and add safety to few. This is not a easy decision, however, since it means uncontrolled maps access can crash the program.
The general meaning is that concurrent access to the map is unsafe and undefined behavior occurs, causing the program to exit. So if you want to access the map concurrently in a multi-process, you have to provide some sort of synchronization mechanism, typically through read-write lock sync. The Rwmutex implements concurrent access control to the map, which maps and sync. Rwmutex package, you can achieve secure concurrent access to the map, the sample code is as follows:
1 Package Main2 3Import "Sync"4 5 type safemap struct {6Sync.Rwmutex7 Map Map[int]int8 }9 Ten Func Main () { OneSafemap: = Newsafemap (10) A - forI: = 0; I < 100000; i++ { -Go Safemap.writemap (i,i) theGo safemap.Readmap (i) - } - - } + -Func newsafemap (size int) *Safemap { +SM: =New(Safemap) ASm. Map =Make (map[int]int) at returnSM - - } - -Func (SM *safemap) Readmap (Keyint) int { -Sm.Rlock () inValue: = Sm. map[Key] -Sm.Runlock () to returnvalue + } - theFunc (SM *safemap) Writemap (KeyIntvalue int) { *Sm.Lock () $Sm. map[Key] =valuePanax NotoginsengSm.Unlock () -}
However, through the read-write lock control Map of the concurrent access, will lead to certain performance problems, but can guarantee the safe operation of the program, the sacrifice point performance problem is possible.
Reference
Go Official Blog: https://blog.golang.org/go-maps-in-action
Go faq:https://golang.org/doc/faq#atomic_maps
Songleo
Links: https://www.jianshu.com/p/10a998089486
The concurrency of Go language learning--map