欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Go實現(xiàn)map并發(fā)安全的3種方式總結(jié)

 更新時間:2023年10月08日 11:14:51   作者:好像、下雨了  
Go的原生map不是并發(fā)安全的,在多協(xié)程讀寫同一個map的時候,安全性無法得到保障,這篇文章主要給大家總結(jié)介紹了關(guān)于Go實現(xiàn)map并發(fā)安全的3種方式,需要的朋友可以參考下

實現(xiàn)map并發(fā)讀寫線程安全

1. 加鎖

對整個map加上讀寫鎖sync.RWMutex

//keyType為key的類型,valueType為value的類型
type RWMap struct {
	Map map[keyType]valueType
	sync.RWMutex
}
func NewRWMap(capacity int) *RWMap {
	if capacity < 0 {
		capacity = 0
	}
	return &RWMap{
		Map: make(map[keyType]valueType, capacity),
	}
}
//add or update
func (m *RWMap) Set(key keyType, value valueType) {
	m.Lock()
	defer m.Unlock()
	m.Map[key] = value
}
//delete
func (m *RWMap) Delete(key int)  {
	m.Lock()
	defer m.Unlock()
	delete(m.Map, key)
}
//get
func (m *RWMap) Get(key int) valueType {
	m.RLock()
	defer m.RUnlock()
	return m.Map[key]
}

優(yōu)點:解決了問題。

缺點:鎖粒度大。

2. 分片加鎖

一個操作會導(dǎo)致整個map被鎖住,導(dǎo)致性能降低。所以提出了分片思想,將一個map分成幾個片,按片加鎖。

第三方包實現(xiàn):github.com/orcaman/concurrent-map

github上用 map language:go 搜索:

3.4kstar

插曲:注意,如果你的goland ide 版本太老的話,github.com/orcaman/concurrent-map/v2 版本是用不了的:

所以我最后換成VSCode,發(fā)現(xiàn)就沒這個問題了。(因為新版本的GoLand還得繼續(xù)想法子破解)

源碼New方法返回的map,看到key只支持string

// Creates a new concurrent map.
func New[V any]() ConcurrentMap[string, V] {
	return create[string, V](fnv32)
}

Example and usage

package main
import (
	"fmt"
	"time"
	cmap "github.com/orcaman/concurrent-map/v2"
)
func main() {
	m := cmap.New[int]()
	for i := 0; i < 300; i++ {
		go func(i int) {
			m.Set(fmt.Sprintf("%v", i), i*2)  //并發(fā)寫
		}(i)
	}
	time.Sleep(4 * time.Second)
	fmt.Println(len(m.Keys()))
}

execute and output:

PS C:\GoWork\src\asset-manager\mytest> go run main.go
300

并發(fā)寫沒問題。

更多使用示例包里的concurrent_map_test.go里面提供了。

3. sync.Map

標準庫中的 sync.Map是專為 append-only 場景設(shè)計的。

sync.Map在讀多寫少性能比較好,否則并發(fā)性能很差。

Go源碼:

// Map is like a Go map[interface{}]interface{} but is safe for concurrent use
// by multiple goroutines without additional locking or coordination. 
// Loads, stores, and deletes run in amortized constant time. 
//=====自注釋======
sync.Map 很像Go map[interface{}]interface{}。但sync.Map是線程安全的,能被多個協(xié)程在沒有額外的鎖或者協(xié)調(diào)的情況下并發(fā)使用。
Loads,stores,deletes操作都運行在分攤常數(shù)時間內(nèi)。
amortized 平攤的(adj.)英  /??m??ta?zd/
//=====自注釋======
//
// The Map type is specialized. Most code should use a plain Go map instead,
// with separate locking or coordination, for better type safety and to make it
// easier to maintain other invariants along with the map content.
//=====自注釋======
invariants (n.) 不變量(invariant的復(fù)數(shù))/?n?veri?nts/
sync.Map 類型是為特殊情況專門設(shè)計的。
大多數(shù)代碼都應(yīng)該使用普通的Go map + 單獨的鎖或者協(xié)調(diào) ,這種形式,來獲得更好的類型安全
以及使得在維護映射內(nèi)容的同時維護其他不變量更容易。
//=====自注釋======
//
// The Map type is optimized for two common use cases: (1) when the entry for a given
// key is only ever written once but read many times, as in caches that only grow,
// or (2) when multiple goroutines read, write, and overwrite entries for disjoint
// sets of keys. In these two cases, use of a Map may significantly reduce lock
// contention compared to a Go map paired with a separate Mutex or RWMutex.
//=====自注釋======
disjoint (adj.) 不連貫的,(兩個集合)不相交的 /d?s?d???nt/
sync.Map類型針對兩個常見用例進行了優(yōu)化:
(1)對于一個給定的key,只會寫一次,但是讀很多次,就像在只增長的緩存中一樣。
(2)當多個協(xié)程讀,寫,重寫不相交的keys。
以上兩種情況,相比于使用Go map + Mutex(或者RWMutex),使用sync.Map能顯著減少鎖競爭。
//=====自注釋======
//
// The zero Map is empty and ready for use. A Map must not be copied after first use.
type Map struct {
	mu Mutex
	// read contains the portion of the map's contents that are safe for
	// concurrent access (with or without mu held).
	//
	// The read field itself is always safe to load, but must only be stored with
	// mu held.
	//
	// Entries stored in read may be updated concurrently without mu, but updating
	// a previously-expunged entry requires that the entry be copied to the dirty
	// map and unexpunged with mu held.
	read atomic.Value // readOnly
	// dirty contains the portion of the map's contents that require mu to be
	// held. To ensure that the dirty map can be promoted to the read map quickly,
	// it also includes all of the non-expunged entries in the read map.
//=====自注釋======
expunged (adj.)/?k?sp?nd?/ 被擦去的,被刪掉的
dirty map 包含map內(nèi)容的部分,該部分要求持有mu鎖。為了確保dirty map能快速提升到read map,
它還包括read map 中所有未刪除的項。
//=====自注釋======
	//
	// Expunged entries are not stored in the dirty map. An expunged entry in the
	// clean map must be unexpunged and added to the dirty map before a new value
	// can be stored to it.
	//
	// If the dirty map is nil, the next write to the map will initialize it by
	// making a shallow copy of the clean map, omitting stale entries.
	dirty map[any]*entry
	// misses counts the number of loads since the read map was last updated that
	// needed to lock mu to determine whether the key was present.
	//
	// Once enough misses have occurred to cover the cost of copying the dirty
	// map, the dirty map will be promoted to the read map (in the unamended
	// state) and the next store to the map will make a new dirty copy.
	misses int
}

read atomic.Value

sync/stomic包里都是go提供的原子操作。

sync.Map思想:就是用兩個數(shù)據(jù)結(jié)構(gòu)(只讀的 read 和可寫的 dirty)盡量將讀寫操作分開,并最小粒度加鎖,來減少鎖對性能的影響。

總結(jié)

較常使用的是前兩種:加讀寫鎖和分片加鎖。特定場景下sync.Map性能會有更優(yōu)的表現(xiàn)(要滿足那兩個場景條件比較苛刻,實際很少用)。

相關(guān)文章

  • Go語言中實現(xiàn)Unix風(fēng)格的進程管道方法實例

    Go語言中實現(xiàn)Unix風(fēng)格的進程管道方法實例

    這篇文章主要為大家介紹了Go語言中實現(xiàn)Unix風(fēng)格的進程管道方法實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • 一文帶你深入了解Golang中的Mutex

    一文帶你深入了解Golang中的Mutex

    這篇文章主要為大家詳細介紹了Golang中Mutex的相關(guān)知識,知其然,更要知其所以然。文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-03-03
  • Go打包附件內(nèi)容到執(zhí)行文件的方法

    Go打包附件內(nèi)容到執(zhí)行文件的方法

    處于種種原因, 我們不希望這部分額外的內(nèi)容以附件的形式出現(xiàn), 有沒有什么辦法能夠?qū)⒏郊?nèi)容直接打包進可執(zhí)行文件中呢,下面小編給大家介紹下Go打包附件內(nèi)容到執(zhí)行文件的方法,感興趣的朋友一起看看吧
    2023-03-03
  • Golang Cron 定時任務(wù)的實現(xiàn)示例

    Golang Cron 定時任務(wù)的實現(xiàn)示例

    這篇文章主要介紹了Golang Cron 定時任務(wù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • go實現(xiàn)冒泡排序算法

    go實現(xiàn)冒泡排序算法

    冒泡排序算法是數(shù)據(jù)結(jié)構(gòu)中常用的一種算法,本文就介紹了go實現(xiàn)冒泡排序算法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 使用Go語言開發(fā)自動化API測試工具詳解

    使用Go語言開發(fā)自動化API測試工具詳解

    這篇文章主要為大家詳細介紹了如何使用Go語言開發(fā)自動化API測試工具,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下
    2024-03-03
  • golang并發(fā)編程中Goroutine 協(xié)程的實現(xiàn)

    golang并發(fā)編程中Goroutine 協(xié)程的實現(xiàn)

    Go語言中的協(xié)程是一種輕量級線程,通過在函數(shù)前加go關(guān)鍵字來并發(fā)執(zhí)行,具有動態(tài)棧、快速啟動和低內(nèi)存使用等特點,本文就來詳細的介紹一下,感興趣的可以了解一下
    2024-10-10
  • 深入了解Golang包的獲取方法

    深入了解Golang包的獲取方法

    Go語言有一個獲取遠程包的工具就是go get,本文將詳細為大家介紹一下Go語言包的獲取的方法,文中的示例代碼講解詳細,感興趣的可以了解一下
    2022-07-07
  • 淺談go build后加文件和目錄的區(qū)別

    淺談go build后加文件和目錄的區(qū)別

    這篇文章主要介紹了淺談go build后加文件和目錄的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Go語言中序列化與反序列化示例詳解

    Go語言中序列化與反序列化示例詳解

    我們的數(shù)據(jù)對象要在網(wǎng)絡(luò)中傳輸或保存到文件,就需要對其編碼和解碼動作,Go語言當然也支持所有這些編碼格式,下面這篇文章主要給大家介紹了關(guān)于Go語言中序列化與反序列化的相關(guān)資料,需要的朋友可以參考下
    2022-07-07

最新評論