25 lines
395 B
Go
25 lines
395 B
Go
|
package pool
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type Worker struct {
|
||
|
id int
|
||
|
queue chan Task
|
||
|
pool *TaskPool // back reference to the pool
|
||
|
}
|
||
|
|
||
|
func NewWorker(id int, queue chan Task, pool *TaskPool) *Worker {
|
||
|
return &Worker{id: id, queue: queue, pool: pool}
|
||
|
}
|
||
|
|
||
|
func (w *Worker) Start(wg *sync.WaitGroup) {
|
||
|
defer wg.Done()
|
||
|
|
||
|
for task := range w.queue {
|
||
|
task.f()
|
||
|
w.pool.MarkTaskComplete(task.id)
|
||
|
}
|
||
|
}
|