Created
April 8, 2015 10:37
-
-
Save mattn/d39b1fc9ff784d0f5cdc to your computer and use it in GitHub Desktop.
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" | |
"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