Created
April 3, 2015 01:30
-
-
Save changtimwu/c55ef514761b0e56188f to your computer and use it in GitHub Desktop.
suture example
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" | |
import "github.com/thejerf/suture" | |
import "time" | |
type FIB struct { | |
last int | |
current int | |
chans chan int | |
stop chan bool | |
} | |
func NewFIB() *FIB { | |
return &FIB{1, 1, make(chan int), make(chan bool)} | |
} | |
func (i *FIB) Get() int { | |
return <-i.chans | |
} | |
func (i *FIB) Stop() { | |
fmt.Println("Stopping the service") | |
i.stop <- true | |
} | |
func (i *FIB) Serve() { | |
fmt.Println("FIB Serve starts") | |
buf := make([]int, 5) | |
bufidx := 0 | |
i.current = 0 | |
for { | |
select { | |
case i.chans <- i.current: | |
nextval := i.last + i.current | |
i.last = i.current | |
i.current = nextval | |
buf[bufidx] = i.current | |
bufidx++ | |
//i.current += 1 | |
case <-i.stop: | |
return | |
} | |
} | |
} | |
func main() { | |
supervisor := suture.NewSimple("Supervisor") | |
fiba := NewFIB() | |
supervisor.Add(fiba) | |
fibb := NewFIB() | |
supervisor.Add(fibb) | |
go supervisor.ServeBackground() | |
for i := 0; i < 20; i++ { | |
<-time.Tick(time.Second / 2) | |
if (i % 4) == 0 { | |
fmt.Println("Got from A:", fiba.Get()) | |
} else { | |
fmt.Println("Got from B:", fibb.Get()) | |
} | |
} | |
var i int | |
fmt.Scan(&i) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment