From 386f0be94b49431a9270d12602346893d4f46184 Mon Sep 17 00:00:00 2001 From: Ying Zou Date: Wed, 4 Apr 2018 14:30:05 +0800 Subject: [PATCH] fix: data race when go func() in for loop --- cache_test.go | 3 ++- sharded_test.go | 6 ++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cache_test.go b/cache_test.go index 47a3d53..8c23469 100644 --- a/cache_test.go +++ b/cache_test.go @@ -1563,9 +1563,10 @@ func benchmarkCacheGetManyConcurrent(b *testing.B, exp time.Duration) { wg := new(sync.WaitGroup) wg.Add(n) for _, v := range keys { + k := v go func() { for j := 0; j < each; j++ { - tc.Get(v) + tc.Get(k) } wg.Done() }() diff --git a/sharded_test.go b/sharded_test.go index aef8597..a6999fa 100644 --- a/sharded_test.go +++ b/sharded_test.go @@ -7,9 +7,6 @@ import ( "time" ) -// func TestDjb33(t *testing.T) { -// } - var shardedKeys = []string{ "f", "fo", @@ -73,9 +70,10 @@ func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) { wg := new(sync.WaitGroup) wg.Add(n) for _, v := range keys { + k := v go func() { for j := 0; j < each; j++ { - tsc.Get(v) + tsc.Get(k) } wg.Done() }()