Skip to content

Instantly share code, notes, and snippets.

@reusee
Created September 13, 2012 04:45
Show Gist options
  • Select an option

  • Save reusee/3711912 to your computer and use it in GitHub Desktop.

Select an option

Save reusee/3711912 to your computer and use it in GitHub Desktop.
ring
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