Skip to content

Instantly share code, notes, and snippets.

@ryochack
Created April 21, 2013 06:54
Show Gist options
  • Select an option

  • Save ryochack/5428734 to your computer and use it in GitHub Desktop.

Select an option

Save ryochack/5428734 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync/atomic"
)
func main() {
const num = 1000
var nRoutines int32 = 0
ch := make(chan int, 1)
for i := 0; i < num; i++ {
// sender
atomic.AddInt32(&nRoutines, 1)
go func(v int, c chan<- int) {
c <- v
atomic.AddInt32(&nRoutines, -1)
} (i, ch)
}
for atomic.LoadInt32(&nRoutines) > 1 {
augend := <-ch
addend := <-ch
// adder
atomic.AddInt32(&nRoutines, 1)
go func(augend, addend int, c chan<- int) {
c <- (augend + addend)
atomic.AddInt32(&nRoutines, -1)
} (augend, addend, ch)
}
fmt.Println(<-ch)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment