From e8ccc8eccadfa5efaddea16b781a8fd1e954abd7 Mon Sep 17 00:00:00 2001 From: AndyPan Date: Sun, 30 Sep 2018 09:40:33 +0800 Subject: [PATCH 1/2] format --- pool.go | 4 ++-- pool_func.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pool.go b/pool.go index 8fee19f..ac575b8 100644 --- a/pool.go +++ b/pool.go @@ -196,10 +196,10 @@ func (p *Pool) getWorker() *Worker { } if waiting { - for{ + for { p.cond.Wait() l := len(p.workers) - 1 - if l < 0{ + if l < 0 { continue } w = p.workers[l] diff --git a/pool_func.go b/pool_func.go index f0c75e5..074710e 100644 --- a/pool_func.go +++ b/pool_func.go @@ -198,10 +198,10 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc { } if waiting { - for{ + for { p.cond.Wait() l := len(p.workers) - 1 - if l < 0{ + if l < 0 { continue } w = p.workers[l] From e538683d3f8d6b8711385c8e7fd63a1c751551d2 Mon Sep 17 00:00:00 2001 From: AndyPan Date: Sun, 30 Sep 2018 09:41:47 +0800 Subject: [PATCH 2/2] add comments --- pool.go | 3 +++ pool_func.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/pool.go b/pool.go index ac575b8..77ec952 100644 --- a/pool.go +++ b/pool.go @@ -52,7 +52,10 @@ type Pool struct { // lock for synchronous operation. lock sync.Mutex + + // cond for waiting idle worker cond *sync.Cond + once sync.Once } diff --git a/pool_func.go b/pool_func.go index 074710e..fc3c73f 100644 --- a/pool_func.go +++ b/pool_func.go @@ -50,7 +50,10 @@ type PoolWithFunc struct { // lock for synchronous operation. lock sync.Mutex + + // cond for waiting idle worker cond *sync.Cond + // pf is the function for processing tasks. poolFunc pf