2024-01-06 01:50:20 +08:00
|
|
|
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 {
|
2024-02-15 12:53:57 +08:00
|
|
|
val, err := task.f()
|
|
|
|
w.pool.markTaskComplete(task.id, WaitBuf{Value: val, Error: err})
|
2024-01-06 01:50:20 +08:00
|
|
|
}
|
|
|
|
}
|