This commit is contained in:
Andy Pan 2018-05-20 01:38:50 +08:00
parent 3a8cc09373
commit a59f51fefd
3 changed files with 5 additions and 7 deletions

View File

@ -1,8 +1,6 @@
package ants package ants
import "math" const DEFAULT_POOL_SIZE = 10000
const DEFAULT_POOL_SIZE = math.MaxInt32
var defaultPool = NewPool(DEFAULT_POOL_SIZE) var defaultPool = NewPool(DEFAULT_POOL_SIZE)

View File

@ -105,18 +105,18 @@ func (p *Pool) newWorker() *Worker {
exit: make(chan sig), exit: make(chan sig),
} }
worker.run() worker.run()
atomic.AddInt32(&p.running, 1)
return worker return worker
} }
func (p *Pool) getWorker() *Worker { func (p *Pool) getWorker() *Worker {
defer atomic.AddInt32(&p.running, 1)
if w := p.workers.pop(); w != nil { if w := p.workers.pop(); w != nil {
return w.(*Worker) return w.(*Worker)
} }
return p.newWorker() return p.newWorker()
} }
func (p *Pool) PutWorker(worker *Worker) { func (p *Pool) putWorker(worker *Worker) {
p.workers.push(worker) p.workers.push(worker)
if p.reachLimit() { if p.reachLimit() {
p.freeSignal <- sig{} p.freeSignal <- sig{}

View File

@ -18,7 +18,7 @@ func (w *Worker) run() {
select { select {
case f := <-w.task: case f := <-w.task:
f() f()
w.pool.workers.push(w) w.pool.putWorker(w)
w.pool.wg.Done() w.pool.wg.Done()
case <-w.exit: case <-w.exit:
atomic.AddInt32(&w.pool.running, -1) atomic.AddInt32(&w.pool.running, -1)
@ -58,7 +58,7 @@ func (q *ConcurrentQueue) push(v interface{}) {
func (q *ConcurrentQueue) pop() interface{} { func (q *ConcurrentQueue) pop() interface{} {
defer q.m.Unlock() defer q.m.Unlock()
q.m.Lock() q.m.Lock()
if elem := q.queue.Back(); elem != nil{ if elem := q.queue.Back(); elem != nil {
return q.queue.Remove(elem) return q.queue.Remove(elem)
} }
return nil return nil