Пример #1
0
func main() {

	var (
		w1 = NewWorker("Worker 1")
		w2 = NewWorker("Worker 2")
		w3 = NewWorker("Worker 3")

		g = killable.New(w1, w2, w3)
	)

	killable.Defer(g, func() {
		fmt.Println("All workers are dead")
	})

	w1.Start()
	w2.Start()
	w3.Start()

	go func() {
		time.Sleep(2 * time.Second)
		fmt.Println("Killing the worker group")
		g.Kill(fmt.Errorf("time to die!"))
	}()

	if err := g.Err(); err != nil {
		log.Println(err)
	}

	// wait for the deferreds to run
	<-g.Dead()
	time.Sleep(time.Second)

}
Пример #2
0
func NewWorker(name string) *Worker {
	return &Worker{
		name:     name,
		ch:       make(chan int64),
		Killable: killable.New(),
	}
}