ants/worker.go

32 lines
368 B
Go
Raw Normal View History

2018-05-19 07:28:03 +03:00
package ants
import (
"sync/atomic"
)
2018-05-19 07:28:03 +03:00
type Worker struct {
pool *Pool
task chan f
}
func (w *Worker) run() {
go func() {
2018-05-19 21:52:39 +03:00
for f := range w.task {
if f == nil {
2018-05-19 13:24:36 +03:00
atomic.AddInt32(&w.pool.running, -1)
2018-05-19 07:28:03 +03:00
return
}
2018-05-19 21:52:39 +03:00
f()
w.pool.putWorker(w)
2018-05-19 07:28:03 +03:00
}
}()
}
func (w *Worker) stop() {
2018-05-19 21:52:39 +03:00
w.task <- nil
2018-05-19 07:28:03 +03:00
}
func (w *Worker) sendTask(task f) {
w.task <- task
}