Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "flag"
- "fmt"
- "sync"
- )
- func main() {
- var nCpu int
- flag.IntVar(&nCpu, "n", 0, "number of parallel procs")
- flag.Parse()
- fmt.Printf("nCPU=%d\n", nCpu)
- wg := sync.WaitGroup{}
- m := sync.Mutex{}
- total := 0
- for i := 0; i < nCpu; i++ {
- wg.Add(1)
- go func(i int) {
- sum := 0
- for j := 0; j < 200000000; j++ {
- sum = (sum + 9876) * j % (2031 + i*i)
- }
- m.Lock()
- total += sum
- fmt.Printf("total(%d)=%d\n", i, sum)
- m.Unlock()
- wg.Done()
- }(i)
- }
- wg.Wait()
- fmt.Printf("%d\n", total)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement