Last active
August 29, 2015 13:56
-
-
Save campoy/8874609 to your computer and use it in GitHub Desktop.
Mandelbrot snippets from github.com/campoy/mandelbrot, for a post on blog.campoy.cat
This file contains hidden or 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
| func fillImg(m *img) { | |
| // init a WaitGroup with the number of pixels of the image | |
| var wg sync.WaitGroup | |
| wg.Add(m.h * m.w) | |
| for i, row := range m.m { | |
| for j := range row { | |
| // Every pixel is computed in a different goroutine | |
| // and the counter is decreased once it's done. | |
| go func(i, j int) { | |
| fillPixel(m, i, j) | |
| wg.Done() | |
| }(i, j) | |
| } | |
| } | |
| wg.Wait() | |
| } |
This file contains hidden or 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
| func fillImg(m *img) { | |
| for i, row := range m.m { | |
| for j := range row { | |
| fillPixel(m, i, j) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| func fillImg(m *img) { | |
| // Channel of pixels, or tasks if you prefer. | |
| c := make(chan struct{ i, j int }) | |
| // We start nWorkers reading from the channel. | |
| // When the channel is closed they all exit. | |
| for i := 0; i < nWorkers; i++ { | |
| go func() { | |
| for t := range c { | |
| fillPixel(m, t.i, t.j) | |
| } | |
| }() | |
| } | |
| // Then we send every pixel into the channel | |
| for i, row := range m.m { | |
| for j := range row { | |
| c <- struct{ i, j int }{i, j} | |
| } | |
| } | |
| // At the end we close the channel so all goroutines exit. | |
| close(c) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment