SHA dir refactoring: routines runner

This commit is contained in:
Meutel 2017-07-08 17:32:21 +02:00
parent 5a237532fa
commit 6b2182b5d4

View File

@ -19,6 +19,16 @@ func sha256File(path string) (string, error) {
return fmt.Sprintf("SHA256 (%s) = %x", path, h.Sum(nil)), nil
}
func runParallel(n uint, what func()) {
var wg sync.WaitGroup
for i := 0; i < int(n); i++ {
wg.Add(1)
what()
wg.Done()
}
wg.Wait()
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Usage shadir file")
@ -26,10 +36,8 @@ func main() {
chan_hash, chan_path := make(chan string), make(chan string)
go func() { // 100 routines hashing
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
runParallel(100, func() { // 100 routines hashing
for path := range chan_path {
if h, err := sha256File(path); err != nil {
log.Print("Error "+path, err)
@ -37,9 +45,7 @@ func main() {
chan_hash <- h
}
}
wg.Done()
}
wg.Wait()
})
close(chan_hash)
}()