Created
September 13, 2012 04:46
-
-
Save reusee/3711915 to your computer and use it in GitHub Desktop.
ring
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" | |
| const links int = 100 | |
| const messages int = 1000000 | |
| type Msg struct { | |
| tag string | |
| msg int | |
| } | |
| func main() { | |
| fmt.Printf("") | |
| chans := make([]chan *Msg, links) | |
| for i := 0; i < links; i++ { | |
| chans[i] = make(chan *Msg) | |
| } | |
| end := make(chan bool) | |
| for i := 0; i < links - 1; i++ { | |
| go start(chans[i], chans[i + 1], end) | |
| } | |
| go start(chans[links - 1], chans[0], end) | |
| chans[0] <- &Msg{"start", messages} | |
| <-end | |
| } | |
| func start(listen chan *Msg, send chan *Msg, end chan bool) { | |
| for { | |
| receive := <-listen | |
| switch receive.tag { | |
| case "start": | |
| send <- &Msg{"tick", receive.msg} | |
| case "tick": | |
| if receive.msg == 0 { | |
| end <- true | |
| } | |
| //fmt.Printf("%d\n", receive.msg) | |
| send <- &Msg{"tick", receive.msg - 1} | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment