Skip to content

Instantly share code, notes, and snippets.

@sheremetyev
Created February 14, 2011 00:40
Show Gist options
  • Save sheremetyev/825346 to your computer and use it in GitHub Desktop.
Save sheremetyev/825346 to your computer and use it in GitHub Desktop.
Parallel execution in Go
package main
import fmt "fmt"
import runtime "runtime"
func parallel(f ... func()) {
c := make(chan int, len(f))
for i := 0; i < len(f); i++ {
f := f[i]
go func() {
f()
c <- 0
}()
}
for i := 0; i < len(f); i++ {
<- c
}
}
func run1() {
for i := 1; i < 1000 ; i++ {
fmt.Printf("run1 %v\n", i)
}
}
func run2() {
for i := 1; i < 1000 ; i++ {
fmt.Printf("run2 %v\n", i)
}
}
func main() {
runtime.GOMAXPROCS(3)
parallel(run1, run2)
fmt.Println("Finished")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment