Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"sync"
"time"
)
func main() {
done := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
go func(id int, done chan struct{}, waiter *sync.WaitGroup) {
for {
fmt.Printf("#%d running\n", id)
time.Sleep(100 * time.Millisecond)
select {
case _ = <-done:
fmt.Printf("#%d exitting.\n", id)
wg.Done()
return
default:
}
}
}(i, done, &wg)
}
wg.Add(4)
time.Sleep(1000 * time.Millisecond)
close(done)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment