Skip to content

Instantly share code, notes, and snippets.

@mattn
Created April 8, 2015 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/d39b1fc9ff784d0f5cdc to your computer and use it in GitHub Desktop.
Save mattn/d39b1fc9ff784d0f5cdc to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
)
var m sync.Mutex
func main() {
cPop := make(chan int, 1)
cPush := make(chan int, 1)
cCache := make(chan int, 100)
go func() {
for {
select {
case cCache <- <-cPush:
m.Lock()
fmt.Println("cCache <- <- cPush")
m.Unlock()
}
}
}()
go func() {
for {
select {
case cPop <- <-cCache:
m.Lock()
fmt.Println("cPop <- <- cCache")
m.Unlock()
}
}
}()
go func() {
for {
select {
case cPush <- <-cPop: // throw the ball again
m.Lock()
fmt.Println("cPush <- <- cPop")
m.Unlock()
}
}
}()
n := 1
cPush <- n
fmt.Println("Push:", n) // throw ball in the endless-loop world
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment