Created
December 22, 2021 15:03
-
-
Save vasily-kirichenko/da57c3284220349fc04f54000278faf3 to your computer and use it in GitHub Desktop.
This file contains 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
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