golang sync.Pool 指針數(shù)據(jù)覆蓋問題解決
場(chǎng)景
1. sync.Pool設(shè)置
var stringPool = sync.Pool{ New: func() any { return new([]string) }, } func NewString() *[]string { v := stringPool.Get().(*[]string) return v } func PutString(s *[]string) { if s == nil { return } if cap(*s) > 2048 { s = nil } else { *s = (*s)[:0] stringPool.Put(s) } }
2.使用sync.Pool
func Test_Pool(t *testing.T) { dataSlice1 := demoData() dataSlice2 := demoData() dataSlice2[1] = "test4" fmt.Printf("dataSlice1:%v %p,dataSlice2:%v %p\n", dataSlice1, dataSlice1, dataSlice2, dataSlice2) } func demoData() []string { strsPtr := NewString() strs := *strsPtr defer func() { *strsPtr = strs PutString(strsPtr) }() strs = append(strs, "test1", "test2") return strs }
打印結(jié)果:dataSlice1:[test1 test4] 0xc0000a6400,dataSlice2:[test1 test4] 0xc0000a6400
可以看到兩個(gè)slice地址相同,內(nèi)部使用同一個(gè)地址的數(shù)組,導(dǎo)致兩次獲取的數(shù)據(jù)互相影響
3.解決方法1
func Test_Pool(t *testing.T) { dataSlice1 := demoData() dataSlice2 := demoData() dataSlice2[1] = "test4" fmt.Printf("dataSlice1:%v %p,dataSlice2:%v %p\n", dataSlice1, dataSlice1, dataSlice2, dataSlice2) } func demoData() []string { strsPtr := NewString() strs := *strsPtr defer func() { *strsPtr = strs PutString(strsPtr) }() strs = append(strs, "test1", "test2") // 深復(fù)制 var items = make([]string, len(strs)) copy(items, strs) return items }
使用深復(fù)制,在put回sync.Pool中之前把數(shù)據(jù)復(fù)制返回,但這樣資源池失去了意義,獲取到資源后有進(jìn)行了一次內(nèi)存的申請(qǐng)
4.解決方法2
我們看下golang語言源碼怎么解決的
參考 go/src/fmt/print.go 302行 Fprintln方法
func Fprintln(w io.Writer, a ...any) (n int, err error) { p := newPrinter() p.doPrintln(a) n, err = w.Write(p.buf) p.free() return }
可以看到306行有p.free()代碼,newPrinter()和free()之間進(jìn)行數(shù)據(jù)處理,數(shù)據(jù)處理完成之后再把資源返回給sync.Pool
總結(jié)
不是任何場(chǎng)景都適合用sync.Pool,需要關(guān)注并發(fā)情況下資源池中數(shù)據(jù)同步修改影響的問題。
到此這篇關(guān)于golang sync.Pool 指針數(shù)據(jù)覆蓋問題解決的文章就介紹到這了,更多相關(guān)golang sync.Pool 指針覆蓋內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang Printf,Sprintf,Fprintf 格式化詳解
這篇文章主要介紹了Golang Printf,Sprintf,Fprintf 格式化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03Golang實(shí)現(xiàn)驗(yàn)證一個(gè)字符串是否為URL
在實(shí)際開發(fā)過程中,有時(shí)候會(huì)遇到?URL?的校驗(yàn)問題,Go?語言中有哪些方法去驗(yàn)證一個(gè)字符串是否滿足?URL?格式呢?本文就來和大家詳細(xì)講講2023-04-04Go語言中init函數(shù)與匿名函數(shù)使用淺析
這篇文章主要介紹了Go語言中init函數(shù)與匿名函數(shù)使用淺析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01詳解Go語言中用 os/exec 執(zhí)行命令的五種方法
這篇文章主要介紹了Go語言中用 os/exec 執(zhí)行命令的五種方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11golang通過context控制并發(fā)的應(yīng)用場(chǎng)景實(shí)現(xiàn)
這篇文章主要介紹了golang通過context控制并發(fā)的應(yīng)用場(chǎng)景實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Golang實(shí)現(xiàn)HTTP代理突破IP訪問限制的步驟詳解
在當(dāng)今互聯(lián)網(wǎng)時(shí)代,網(wǎng)站和服務(wù)商為了維護(hù)安全性和保護(hù)用戶隱私,常常會(huì)對(duì)特定的IP地址進(jìn)行封鎖或限制,本文將介紹如何使用Golang實(shí)現(xiàn)HTTP代理來突破IP訪問限制,需要的朋友可以參考下2023-10-10