写了一段并发代码,初始化一个10w个元素的int数组,输出这10w个数之和,我的机器是2个cpu,每个cpu包含6个核,将数组分成12分子数组,分别用12个goroutine来算出子数组的和,最后求总和。
goroutine的执行单位是calsome()函数,我在这个函数的最后故意加了个死循环,结果执行时有时能安全退出(即输出result=4999950000),有时输出了若干个from=%d, subSum=%d就夯住了(此时通过top命令看到各个cpu核使用率都几乎达到100%),哪位大神能帮忙解释这种情况?
完整代码如下:
package main
import (
"fmt"
"runtime"
"time"
)
const COUNT int = 100000
const SIZE int = 100000
var MULTICORE int
func main() {
var num [SIZE]int
MULTICORE = runtime.NumCPU() //number of core
runtime.GOMAXPROCS(MULTICORE) //running in multicore
for i := 0; i < COUNT; i++ {
num[i] = i
}
fmt.Printf("result=%d\n", calmul(num[0:]))
}
func calmul(num []int) int {
t1 := time.Now()
ch := make(chan int)
fmt.Printf("with %d core\n", MULTICORE)
for i := 0; i < MULTICORE; i++ {
go calsome(i*COUNT/MULTICORE, (i+1)*COUNT/MULTICORE, num[0:], ch, i)
} //divide into some parts
result := 0;
for i := 0; i < MULTICORE; i++ {
temp := <-ch
result += temp
} //read result of some part from channel,loop will stop after all is read
t2 := time.Now()
fmt.Printf("multicore total time:%d\n", t2.Sub(t1))
return result
}
func calsome(from int, to int, num []int, ch chan int, i int) {
subSum := 0
for i := from; i < to; i++ {
subSum += num[i]
}
fmt.Printf("from=%d, subSum=%d\n", from,subSum);
ch <- subSum //put result in channel
j := 1;
**//这里做了死循环**
for j > 0{}
}
有疑问加站长微信联系(非本文作者)