diff --git a/pool.go b/pool.go index 8fee19f..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 } @@ -196,10 +199,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..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 @@ -198,10 +201,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]