From 70731aff71f7750e34ccc0dcf878fba95614ddd4 Mon Sep 17 00:00:00 2001 From: liyonglion <470542519@qq.com> Date: Fri, 28 Sep 2018 19:28:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=AD=BB=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E5=AF=BC=E8=87=B4cpu=E5=8D=A0=E7=94=A8=E7=8E=87=E8=BF=87?= =?UTF-8?q?=E9=AB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pool.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/pool.go b/pool.go index 987ba4d..5f6ce63 100644 --- a/pool.go +++ b/pool.go @@ -52,7 +52,7 @@ type Pool struct { // lock for synchronous operation. lock sync.Mutex - + cond *sync.Cond once sync.Once } @@ -105,6 +105,7 @@ func NewTimingPool(size, expiry int) (*Pool, error) { release: make(chan sig, 1), expiryDuration: time.Duration(expiry) * time.Second, } + p.cond = sync.NewCond(&p.lock) go p.periodicallyPurge() return p, nil } @@ -195,20 +196,11 @@ func (p *Pool) getWorker() *Worker { p.lock.Unlock() if waiting { - for { - p.lock.Lock() - idleWorkers = p.workers - l := len(idleWorkers) - 1 - if l < 0 { - p.lock.Unlock() - continue - } - w = idleWorkers[l] - idleWorkers[l] = nil - p.workers = idleWorkers[:l] - p.lock.Unlock() - break - } + p.lock.Lock() + p.cond.Wait() + l := len(p.workers) - 1 + w = p.workers[l] + p.lock.Unlock() } else if w == nil { w = &Worker{ pool: p, @@ -226,4 +218,6 @@ func (p *Pool) putWorker(worker *Worker) { p.lock.Lock() p.workers = append(p.workers, worker) p.lock.Unlock() + //通知有一个空闲的worker + p.cond.Signal() }