Skip to content

Instantly share code, notes, and snippets.

@yumed15
Created November 22, 2023 12:17
Show Gist options
  • Save yumed15/aeeb07273f92b5595c143bb1078f5b1d to your computer and use it in GitHub Desktop.
Save yumed15/aeeb07273f92b5595c143bb1078f5b1d to your computer and use it in GitHub Desktop.
var numCalcsCreated int
calclPool := &sync.Pool{
New: func() interface{}{
numCalcsCreated += 1
mem := make([]byte, 1024)
return &mem
},
}
// Seed the pool with 4KB
calclPool.Put(calclPool.New())
calclPool.Put(calclPool.New())
calclPool.Put(calclPool.New())
calclPool.Put(calclPool.New())
const numWorkers = 1024*1024
var wg sync.WaitGroup
wg.Add(numWorkers)
for i:=numWorkers; i>0; i-- {
go func() {
defer wg.Done()
mem := calcPool.Get().(*[]byte)
defer calcPool.Put(mem)
}()
}
wg.Wait()
fmt.Printf(numCalcsCreated) // 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment