Created
October 5, 2016 16:11
-
-
Save paulja/2d8d76558fdcf6bbb0e646d01fd1b298 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"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