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
import "math"
const DEFAULT_POOL_SIZE = math.MaxInt32
const DEFAULT_POOL_SIZE = 10000
var defaultPool = NewPool(DEFAULT_POOL_SIZE)

View File

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

View File

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