Created
March 3, 2014 22:31
-
-
Save benlemasurier/9336022 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func main() { | |
const n = 100 | |
finish := make(chan bool) | |
var done sync.WaitGroup | |
for i := 0; i < n; i++ { | |
done.Add(1) | |
go func() { | |
select { | |
case <-time.After(1 * time.Hour): | |
case <-finish: | |
} | |
done.Done() | |
}() | |
} | |
t0 := time.Now() | |
close(finish) // closing finish makes it ready to receive | |
done.Wait() // wait for all goroutines to stop | |
fmt.Printf("Waited %v for %d goroutines to stop\n", time.Since(t0), n) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment