Created
October 14, 2014 15:20
-
-
Save border/2761f136cc5922db589c to your computer and use it in GitHub Desktop.
Golang chan test
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 ( | |
"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