快速解決Golang Map 并發(fā)讀寫(xiě)安全的問(wèn)題
一、錯(cuò)誤案例
package main
import (
"fmt"
"time"
)
var TestMap map[string]string
func init() {
TestMap = make(map[string]string, 1)
}
func main() {
for i := 0; i < 1000; i++ {
go Write("aaa")
go Read("aaa")
go Write("bbb")
go Read("bbb")
}
time.Sleep(5 * time.Second)
}
func Read(key string) {
fmt.Println(TestMap[key])
}
func Write(key string) {
TestMap[key] = key
}
上面代碼執(zhí)行大概率出現(xiàn)報(bào)錯(cuò):fatal error: concurrent map writes
二、問(wèn)題分析
網(wǎng)上關(guān)于 golang 編程中 map 并發(fā)讀寫(xiě)相關(guān)的資料很多,但總是都說(shuō)成 并發(fā)讀寫(xiě) 造成上面的錯(cuò)誤,到底是 并發(fā)讀 還是 并發(fā)寫(xiě) 造成的,這個(gè)很多資料都沒(méi)有說(shuō)明。
我們把上面的案例分別在循環(huán)中注釋 Read 和 Write 函數(shù)的調(diào)用,分別測(cè)試 并發(fā)讀 和 并發(fā)寫(xiě);
循環(huán)次數(shù)分別測(cè)試了 100、1 w、100 w 次,并發(fā)讀操作絕對(duì)不會(huì)報(bào)上面的錯(cuò),而并發(fā)寫(xiě)基本都會(huì)報(bào)錯(cuò)。
因此,這個(gè)錯(cuò)誤主要原因是:map 并發(fā)寫(xiě)。
三、問(wèn)題原因
為什么 map 并發(fā)寫(xiě)會(huì)導(dǎo)致這個(gè)錯(cuò)誤? 網(wǎng)絡(luò)上的相關(guān)文章也大都有說(shuō)明。
因?yàn)?map 變量為 指針類(lèi)型變量,并發(fā)寫(xiě)時(shí),多個(gè)協(xié)程同時(shí)操作一個(gè)內(nèi)存,類(lèi)似于多線程操作同一個(gè)資源會(huì)發(fā)生競(jìng)爭(zhēng)關(guān)系,共享資源會(huì)遭到破壞,因此golang 出于安全的考慮,拋出致命錯(cuò)誤:fatal error: concurrent map writes。
四、解決方案
網(wǎng)上各路資料解決方案較多,主要思路是通過(guò)加鎖保證每個(gè)協(xié)程同步操作內(nèi)存。
github 上找到一個(gè) concurrentMap 包,案例代碼修改如下:
package main
import (
"fmt"
cmap "github.com/orcaman/concurrent-map"
"time"
)
var TestMap cmap.ConcurrentMap
func init() {
TestMap = cmap.New()
}
func main() {
for i := 0; i < 100; i++ {
go Write("aaa", "111")
go Read("aaa")
go Write("bbb", "222")
go Read("bbb")
}
time.Sleep(5 * time.Second)
}
func Read(key string) {
if v, ok := TestMap.Get(key); ok {
fmt.Printf("鍵值為 %s 的值為:%s", key, v)
} else {
fmt.Printf("鍵值不存在")
}
}
func Write(key string, value string) {
TestMap.Set(key, value)
}
五、思考總結(jié)
因?yàn)槲沂且?PHP 打開(kāi)的編程世界,PHP 語(yǔ)言只有單線程,且不涉及指針操作,變量類(lèi)型也是弱變量,以 PHP 編程思維剛開(kāi)始接觸 Golang 時(shí)還比較容易上手,但越往后,語(yǔ)言的特性區(qū)別就體現(xiàn)得越來(lái)越明顯,思維轉(zhuǎn)變就越來(lái)越大,對(duì)我來(lái)說(shuō)是打開(kāi)了一個(gè)新世界。
像本文出現(xiàn)的錯(cuò)誤案例,也是因?yàn)樽约簺](méi)有多線程編程的思維基礎(chǔ),導(dǎo)致對(duì)這種問(wèn)題不敏感,還是花了蠻多時(shí)間理解的。希望對(duì)和我有相似學(xué)習(xí)路線的朋友提供到一些幫助。
補(bǔ)充:Golang Map并發(fā)處理機(jī)制(sync.Map)
Go語(yǔ)言中的Map在并發(fā)情況下,只讀是線程安全的,同時(shí)讀寫(xiě)線程不安全。
示例:
package main
import (
"fmt"
)
var m = make(map[int]int)
func main() {
//寫(xiě)入操作
i:=0
go func() {
for{
i++
m[1]=i
}
}()
//讀操作
go func() {
for{
fmt.Println(m[1])
}
}()
//無(wú)限循環(huán),讓并發(fā)程序在后臺(tái)運(yùn)行
for {
;
}
}

從以上示例可以看出,不斷地對(duì)map進(jìn)行讀和寫(xiě),會(huì)出現(xiàn)錯(cuò)誤。主要原因是對(duì)map進(jìn)行讀和寫(xiě)發(fā)生了競(jìng)態(tài)問(wèn)題。map內(nèi)部會(huì)對(duì)這種并發(fā)操作進(jìn)行檢查并提前發(fā)現(xiàn)。
如果確實(shí)需要對(duì)map進(jìn)行并發(fā)讀寫(xiě)操作,可以采用加鎖機(jī)制、channel同步機(jī)制,但這樣性能并不高。
Go語(yǔ)言在1.9版本中提供了一種效率較高的并發(fā)安全的sync.Map。
sync.Map結(jié)構(gòu)如下:
The zero Map is empty and ready for use. A Map must not be copied after first use.
type Map struct {
mu Mutex
misses int
}
// Load returns the value stored in the map for a key, or nil if no
// value is present.
// The ok result indicates whether value was found in the map.
func (m *Map) Load(key interface{}) (value interface{}, ok bool) {
}
// Store sets the value for a key.
func (m *Map) Store(key, value interface{}) {
}
// LoadOrStore returns the existing value for the key if present.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false if stored.
func (m *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) {
}
// Delete deletes the value for a key.
func (m *Map) Delete(key interface{}) {
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
//
// Range does not necessarily correspond to any consistent snapshot of the Map's
// contents: no key will be visited more than once, but if the value for any key
// is stored or deleted concurrently, Range may reflect any mapping for that key
// from any point during the Range call.
//
// Range may be O(N) with the number of elements in the map even if f returns
// false after a constant number of calls.
func (m *Map) Range(f func(key, value interface{}) bool) {
}
func (m *Map) missLocked() {
}
func (m *Map) dirtyLocked() {
}
其實(shí),sync.Map內(nèi)部還是進(jìn)行了加鎖機(jī)制,不過(guò)進(jìn)行了一定的優(yōu)化。
sync.Map使用示例:
package main
import (
"fmt"
"sync"
"time"
)
var m1 sync.Map
func main() {
i := 0
go func() {
for {
i++
m1.Store(1, i)
time.Sleep(1000)
}
}()
go func() {
for{
time.Sleep(1000)
fmt.Println(m1.Load(1))
}
}()
for {
;
}
}
成功運(yùn)行效果如下:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Go語(yǔ)言通過(guò)反射實(shí)現(xiàn)獲取各種類(lèi)型變量的值
反射是程序在運(yùn)行期間獲取變量的類(lèi)型和值、或者執(zhí)行變量的方法的能力,這篇文章主要為大家講講Go語(yǔ)言通過(guò)反射獲取各種類(lèi)型變量值的方法,需要的可以參考下2023-07-07
golang如何通過(guò)viper讀取config.yaml文件
這篇文章主要介紹了golang通過(guò)viper讀取config.yaml文件,圍繞golang讀取config.yaml文件的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-03-03
golang使用viper加載配置文件實(shí)現(xiàn)自動(dòng)反序列化到結(jié)構(gòu)
這篇文章主要為大家介紹了golang使用viper加載配置文件實(shí)現(xiàn)自動(dòng)反序列化到結(jié)構(gòu)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
通過(guò)函數(shù)如何將golang?float64?保留2位小數(shù)(方法匯總)
這篇文章主要介紹了通過(guò)函數(shù)將golang?float64保留2位小數(shù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
Golang優(yōu)雅關(guān)閉channel的方法示例
Goroutine和channel是Go在“并發(fā)”方面兩個(gè)核心feature,下面這篇文章主要給大家介紹了關(guān)于Golang如何優(yōu)雅關(guān)閉channel的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考解決,下面來(lái)一起看看吧。2017-11-11

