You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/threading/workergroup.go

25 lines
509 B
Go

4 years ago
package threading
// A WorkerGroup is used to run given number of workers to process jobs.
4 years ago
type WorkerGroup struct {
job func()
workers int
}
// NewWorkerGroup returns a WorkerGroup with given job and workers.
4 years ago
func NewWorkerGroup(job func(), workers int) WorkerGroup {
return WorkerGroup{
job: job,
workers: workers,
}
}
// Start starts a WorkerGroup.
4 years ago
func (wg WorkerGroup) Start() {
group := NewRoutineGroup()
for i := 0; i < wg.workers; i++ {
group.RunSafe(wg.job)
}
group.Wait()
}