Last active
June 21, 2016 01:22
-
-
Save ebenoist/efe4a6fb427064ddcccc9e6ce10e9eef to your computer and use it in GitHub Desktop.
ch9.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 ( | |
"fmt" | |
"os" | |
"strconv" | |
"time" | |
) | |
func main() { | |
num, _ := strconv.Atoi(os.Args[1]) | |
first := make(chan bool) | |
out := make(chan bool) | |
go flipper(first, out) | |
then := time.Now() | |
fmt.Printf("Building a chain with %d goroutines\n", num) | |
var in chan bool | |
for i := 0; i < num; i++ { | |
in = out | |
out = make(chan bool) | |
go flipper(in, out) | |
} | |
fmt.Printf("Chain built in %v\n", time.Now().Sub(then)) | |
then = time.Now() | |
first <- true | |
<-out | |
fmt.Printf("Sent finished in %v\n", time.Now().Sub(then)) | |
} | |
func flipper(in chan bool, out chan bool) { | |
r := <-in | |
out <- !r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment