Created
September 8, 2016 13:19
-
-
Save kdomanski/83951d7b90723fb3e10140d98d250524 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() { | |
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