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.
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()
|
|
}
|