Skip to content

Instantly share code, notes, and snippets.

@vasily-kirichenko
Created December 22, 2021 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasily-kirichenko/da57c3284220349fc04f54000278faf3 to your computer and use it in GitHub Desktop.
Save vasily-kirichenko/da57c3284220349fc04f54000278faf3 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"runtime"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
log.Printf("[computation #%d] start", i)
compute()
log.Printf("[computation #%d] done", i)
}(i)
}
req := make(chan int)
go func() {
for i := range req {
log.Printf("[server] got %d", i)
time.Sleep(time.Second)
}
}()
for i := 0; i < runtime.NumCPU(); i++ {
req <- i
}
close(req)
wg.Wait()
}
func compute() {
fib(44)
}
func fib(n int) int {
if n < 2 {
return n
}
return fib(n-1) + fib(n-2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment