workergroup.go 337 B

123456789101112131415161718192021
  1. package threading
  2. type WorkerGroup struct {
  3. job func()
  4. workers int
  5. }
  6. func NewWorkerGroup(job func(), workers int) WorkerGroup {
  7. return WorkerGroup{
  8. job: job,
  9. workers: workers,
  10. }
  11. }
  12. func (wg WorkerGroup) Start() {
  13. group := NewRoutineGroup()
  14. for i := 0; i < wg.workers; i++ {
  15. group.RunSafe(wg.job)
  16. }
  17. group.Wait()
  18. }