From 0bffe1e4c21a060f17c4f2e3cd097a6f3b4e7920 Mon Sep 17 00:00:00 2001 From: Ramakrishnan G Date: Thu, 7 Feb 2019 10:19:54 +0530 Subject: [PATCH] Make New method thread-safe in fig-sync-pool.go We used a counter to count the number of instances created and that needs to be protected around a mutex to make it thread safe. --- .../the-sync-package/pool/fig-sync-pool.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gos-concurrency-building-blocks/the-sync-package/pool/fig-sync-pool.go b/gos-concurrency-building-blocks/the-sync-package/pool/fig-sync-pool.go index f363c71..dcb1d5e 100644 --- a/gos-concurrency-building-blocks/the-sync-package/pool/fig-sync-pool.go +++ b/gos-concurrency-building-blocks/the-sync-package/pool/fig-sync-pool.go @@ -7,9 +7,12 @@ import ( func main() { var numCalcsCreated int + var m sync.Mutex calcPool := &sync.Pool{ New: func() interface{} { + m.Lock() numCalcsCreated += 1 + m.Unlock() mem := make([]byte, 1024) return &mem // <1> },