Golang-线程在Linux中自动挂起

Golang-线程在Linux中自动挂起,linux,multithreading,go,Linux,Multithreading,Go,我已尝试使用Raspberry PI 2上的所有CPU核并行计算uint32的范围: ... numcpu := runtime.NumCPU() runtime.GOMAXPROCS(numcpu) //4 cores c := make(chan int, numcpu) //buffered channel results := make([]int, numcpu) part := math.MaxUint32/numcpu for i:=0; i<numcpu; i++ {

我已尝试使用Raspberry PI 2上的所有CPU核并行计算uint32的范围:

...
numcpu := runtime.NumCPU()
runtime.GOMAXPROCS(numcpu) //4 cores
c := make(chan int, numcpu) //buffered channel
results := make([]int, numcpu)
part := math.MaxUint32/numcpu

for i:=0; i<numcpu; i++ {
    go calculate(c,i*part,(i+1)*part,i)
}
for i:=0; i<numcpu; i++ {
    results = append(results,<-c)
}
...

func calculate(c chan int, from, to, i int) {
    ... //calculating result
    c<-result
    println("Goroutine #", i, " stopped") //just for debug
}
。。。
numpu:=runtime.numpu()
runtime.GOMAXPROCS(numpu)//4个内核
c:=make(chan int,numpu)//缓冲通道
结果:=make([]int,numpu)
部分:=math.MaxUint32/numpu

对于i:=0;I如果你发布完整的程序,你可能会得到更多的帮助,这样其他人就可以尝试/调试它。这不是你的主要问题,但是当你将结果收集到
结果中时,我认为你不需要
附加
,只要做
结果[I]=我认为程序运行太快,不适合htop。你可以在你的函数中打印纳秒来检查
并行化
细节:
运行时。GOMAXPROCS(numcpu)
是Go1.5之前的版本,甚至在那时它被认为是一个坏习惯。如果你发布完整的程序,你可能会得到更多的帮助,这样其他人可以尝试/调试它。这不是你的主要问题,但是,当您将结果收集到
结果中时
我认为您不需要
追加
,只需执行
结果[I]=我认为,对于htop,程序运行得太快。您可以在函数中打印纳秒来检查
并行化
挑剔:
运行时。GOMAXPROCS(numcpu)
是Go1.5之前的版本,即使在那时它也被认为是一个坏习惯。