Skip to content

Instantly share code, notes, and snippets.

@campoy
Last active Aug 29, 2015
Embed
What would you like to do?
Mandelbrot snippets from github.com/campoy/mandelbrot, for a post on blog.campoy.cat
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()
}
func fillImg(m *img) {
for i, row := range m.m {
for j := range row {
fillPixel(m, i, j)
}
}
}
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