Skip to content

Instantly share code, notes, and snippets.

@paulja
Created October 5, 2016 16:11
Show Gist options
  • Save paulja/2d8d76558fdcf6bbb0e646d01fd1b298 to your computer and use it in GitHub Desktop.
Save paulja/2d8d76558fdcf6bbb0e646d01fd1b298 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func main() {
const (
MAX_WORKERS = 2
WORK_ITEM_COUNT = 3
)
w := []chan bool{}
s := make(chan bool, MAX_WORKERS)
e := make(chan error)
c := make(chan bool)
go func() {
for {
select {
case err := <-e:
fmt.Println("ERROR:", err)
case <-c:
return
}
}
}()
for i := 0; i < WORK_ITEM_COUNT; i++ {
i := i
n := make(chan bool)
w = append(w, n)
s <- true
go func() {
fmt.Println(i, "Start")
if i == 1 {
e <- fmt.Errorf("%d Oops", i)
} else {
time.Sleep(3 * time.Second)
}
fmt.Println(i, "Finish")
<-s
n <- true
}()
}
for _, n := range w {
<-n
}
c <- true
fmt.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment