From daf637e4f72ed4377a38fe05c91dac17d9f17841 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Fri, 15 Jun 2018 08:04:28 +0800 Subject: [PATCH 1/2] optimization for getWorker() --- pool.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/pool.go b/pool.go index ee14834..72fc7e2 100644 --- a/pool.go +++ b/pool.go @@ -140,6 +140,7 @@ func (p *Pool) getWorker() *Worker { p.running++ } } else { + <-p.freeSignal w = workers[n] workers[n] = nil p.workers = workers[:n] @@ -148,20 +149,13 @@ func (p *Pool) getWorker() *Worker { if waiting { <-p.freeSignal - for { - p.lock.Lock() - workers = p.workers - l := len(workers) - 1 - if l < 0 { - p.lock.Unlock() - continue - } - w = workers[l] - workers[l] = nil - p.workers = workers[:l] - p.lock.Unlock() - break - } + p.lock.Lock() + workers = p.workers + l := len(workers) - 1 + w = workers[l] + workers[l] = nil + p.workers = workers[:l] + p.lock.Unlock() } else if w == nil { w = &Worker{ pool: p, From ce8f31e051cc4c78c42d63bc5215c1d31d385eb5 Mon Sep 17 00:00:00 2001 From: andy pan Date: Fri, 15 Jun 2018 11:26:37 +0800 Subject: [PATCH 2/2] Auto stash before merge of "develop" and "origin/develop" --- examples/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/main.go b/examples/main.go index 3d9e26d..413cb36 100644 --- a/examples/main.go +++ b/examples/main.go @@ -34,8 +34,8 @@ import ( var sum int32 func myFunc(i interface{}) error { - n := i.(int) - atomic.AddInt32(&sum, int32(n)) + n := i.(int32) + atomic.AddInt32(&sum, n) fmt.Printf("run with %d\n", n) return nil } @@ -73,7 +73,7 @@ func main() { // submit tasks for i := 0; i < runTimes; i++ { wg.Add(1) - p.Serve(i) + p.Serve(int32(i)) } wg.Wait() fmt.Printf("running goroutines: %d\n", p.Running())