mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
25 lines
509 B
Go
25 lines
509 B
Go
package threading
|
|
|
|
// A WorkerGroup is used to run given number of workers to process jobs.
|
|
type WorkerGroup struct {
|
|
job func()
|
|
workers int
|
|
}
|
|
|
|
// NewWorkerGroup returns a WorkerGroup with given job and workers.
|
|
func NewWorkerGroup(job func(), workers int) WorkerGroup {
|
|
return WorkerGroup{
|
|
job: job,
|
|
workers: workers,
|
|
}
|
|
}
|
|
|
|
// Start starts a WorkerGroup.
|
|
func (wg WorkerGroup) Start() {
|
|
group := NewRoutineGroup()
|
|
for i := 0; i < wg.workers; i++ {
|
|
group.RunSafe(wg.job)
|
|
}
|
|
group.Wait()
|
|
}
|