From 1846b4392a3a20e6bf1a7431b67f86bd43e0f0b9 Mon Sep 17 00:00:00 2001 From: liyonglion <470542519@qq.com> Date: Sat, 29 Sep 2018 14:58:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=AB=9E=E4=BA=89=E9=94=81?= =?UTF-8?q?=E5=AF=BC=E8=87=B4bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ants_benchmark_test.go | 8 ++++---- pool.go | 14 +++++++++++--- pool_func.go | 15 ++++++++++++--- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index b88d8bf..f63d114 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -42,9 +42,9 @@ const ( YiB // 1208925819614629174706176 ) const ( - RunTimes = 10000000 + RunTimes = 1000000 Param = 100 - AntsSize = 1000 + AntsSize = 50000 TestSize = 10000 ) @@ -68,6 +68,7 @@ func demoPoolFunc(args interface{}) error { func BenchmarkGoroutineWithFunc(b *testing.B) { var wg sync.WaitGroup + for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { wg.Add(1) @@ -96,7 +97,7 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) { p.Serve(Param) } wg.Wait() - b.Logf("running goroutines: %d", p.Running()) + //b.Logf("running goroutines: %d", p.Running()) } } @@ -111,7 +112,6 @@ func BenchmarkGoroutine(b *testing.B) { func BenchmarkAntsPool(b *testing.B) { p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc) defer p.Release() - b.ResetTimer() for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { diff --git a/pool.go b/pool.go index f3d931c..8fee19f 100644 --- a/pool.go +++ b/pool.go @@ -196,9 +196,17 @@ func (p *Pool) getWorker() *Worker { } if waiting { - p.cond.Wait() - l := len(p.workers) - 1 - w = p.workers[l] + for{ + p.cond.Wait() + l := len(p.workers) - 1 + if l < 0{ + continue + } + w = p.workers[l] + p.workers[l] = nil + p.workers = p.workers[:l] + break + } } else if w == nil { w = &Worker{ pool: p, diff --git a/pool_func.go b/pool_func.go index d1ed757..f0c75e5 100644 --- a/pool_func.go +++ b/pool_func.go @@ -198,9 +198,18 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc { } if waiting { - p.cond.Wait() - l := len(p.workers) - 1 - w = p.workers[l] + for{ + p.cond.Wait() + l := len(p.workers) - 1 + if l < 0{ + continue + } + w = p.workers[l] + p.workers[l] = nil + p.workers = p.workers[:l] + break + } + } else if w == nil { w = &WorkerWithFunc{ pool: p,