Skip to content

Instantly share code, notes, and snippets.

@border
Created October 14, 2014 15:20
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 border/2761f136cc5922db589c to your computer and use it in GitHub Desktop.
Save border/2761f136cc5922db589c to your computer and use it in GitHub Desktop.
Golang chan test
package main
import (
"runtime"
"sync"
"time"
)
func Generate(ch chan<- int, quit chan bool) {
for i := 2; i < 1000; i++ {
ch <- i
}
close(ch)
close(quit)
}
var s sync.Mutex
func doFileCopy(ctl chan bool, prime int) {
s.Lock()
print(prime, "\n")
s.Unlock()
time.Sleep(3 * time.Second)
ctl <- true
}
func main() {
max_procs := runtime.NumCPU()
runtime.GOMAXPROCS(max_procs - 1)
ch := make(chan int, 5)
quit := make(chan bool)
go Generate(ch, quit)
for i := 0; i < 5; i++ {
go func(c chan int, disk int) {
for {
preDiskCh := make(chan bool, 4)
for j := 0; j < 4; j++ {
prime := <-c
go doFileCopy(preDiskCh, prime)
}
<-preDiskCh
}
}(ch, i)
}
<-quit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment