golang sync.Pool 指針數(shù)據(jù)覆蓋問題解決
場景
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
可以看到兩個slice地址相同,內(nèi)部使用同一個地址的數(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ù)制返回,但這樣資源池失去了意義,獲取到資源后有進行了一次內(nèi)存的申請
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()之間進行數(shù)據(jù)處理,數(shù)據(jù)處理完成之后再把資源返回給sync.Pool
總結(jié)
不是任何場景都適合用sync.Pool,需要關(guān)注并發(fā)情況下資源池中數(shù)據(jù)同步修改影響的問題。
到此這篇關(guān)于golang sync.Pool 指針數(shù)據(jù)覆蓋問題解決的文章就介紹到這了,更多相關(guān)golang sync.Pool 指針覆蓋內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang Printf,Sprintf,Fprintf 格式化詳解
這篇文章主要介紹了Golang Printf,Sprintf,Fprintf 格式化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Go語言中init函數(shù)與匿名函數(shù)使用淺析
這篇文章主要介紹了Go語言中init函數(shù)與匿名函數(shù)使用淺析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
詳解Go語言中用 os/exec 執(zhí)行命令的五種方法
這篇文章主要介紹了Go語言中用 os/exec 執(zhí)行命令的五種方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
golang通過context控制并發(fā)的應(yīng)用場景實現(xiàn)
這篇文章主要介紹了golang通過context控制并發(fā)的應(yīng)用場景實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Golang實現(xiàn)HTTP代理突破IP訪問限制的步驟詳解
在當今互聯(lián)網(wǎng)時代,網(wǎng)站和服務(wù)商為了維護安全性和保護用戶隱私,常常會對特定的IP地址進行封鎖或限制,本文將介紹如何使用Golang實現(xiàn)HTTP代理來突破IP訪問限制,需要的朋友可以參考下2023-10-10

