ants/worker.go

49 lines
639 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
}
2018-05-19 21:52:39 +03:00
//func (w *Worker) run() {
// go func() {
// for {
// select {
// case f := <-w.task:
// f()
// w.pool.putWorker(w)
// w.pool.wg.Done()
// case <-w.exit:
// atomic.AddInt32(&w.pool.running, -1)
// return
// }
// }
// }()
//}
2018-05-19 07:28:03 +03:00
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)
w.pool.wg.Done()
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
}