Permalink
Browse files

different structure and usage of wg (thanks to tip from rog)

  • Loading branch information...
kidoman committed Sep 25, 2013
1 parent 7420ef3 commit ddfe825f0902877c02467a4f65f46c4044bc7939
Showing with 9 additions and 16 deletions.
  1. +9 −16 main.go
View
25 main.go
@@ -84,15 +84,16 @@ func main() {
rows := make(chan row, *height)
wg := &sync.WaitGroup{}
startWorkers(runtime.NumCPU(), &a, &b, &c, bytes, rows, wg)
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go worker(&a, &b, &c, bytes, rows, wg)
}
for y := (*height - 1); y >= 0; y-- {
wg.Add(1)
rows <- row(y)
}
wg.Wait()
close(rows)
wg.Wait()
if _, err := os.Stdout.Write(bytes); err != nil {
log.Panic(err)
@@ -101,18 +102,10 @@ func main() {
type row int
func startWorkers(count int, a, b, c *vector.Vector, bytes []byte, rows <-chan row, wg *sync.WaitGroup) {
for i := 0; i < count; i++ {
go func() {
for {
r, ok := <-rows
if !ok {
return
}
renderRow(a, b, c, bytes, r)
wg.Done()
}
}()
func worker(a, b, c *vector.Vector, bytes []byte, rows <-chan row, wg *sync.WaitGroup) {
defer wg.Done()
for r := range rows {
renderRow(a, b, c, bytes, r)
}
}

0 comments on commit ddfe825

Please sign in to comment.