Created
September 15, 2014 21:41
-
-
Save benschw/09f3a91e8bba7872120a to your computer and use it in GitHub Desktop.
concurent pipeline processing in go
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 ( | |
"log" | |
"strconv" | |
"time" | |
) | |
var _ = log.Print | |
func genWork(queue chan int, sig chan bool) { | |
for i := 0; i < 10; i++ { | |
log.Print("q " + strconv.FormatInt(int64(i), 10)) | |
queue <- i | |
} | |
close(queue) | |
sig <- true | |
} | |
func doWork(queue chan int, sig chan bool) { | |
time.Sleep(1000 * time.Millisecond) | |
for val := range queue { | |
log.Print("d " + strconv.FormatInt(int64(val), 10)) | |
} | |
log.Print("done working") | |
sig <- true | |
} | |
func main() { | |
queue := make(chan int, 20) | |
genSig := make(chan bool, 1) | |
doSig := make(chan bool, 1) | |
go genWork(queue, genSig) | |
go doWork(queue, doSig) | |
log.Print("outer") | |
<-genSig | |
<-doSig | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment