【发布时间】:2014-04-22 03:03:56
【问题描述】:
这是我的代码的相关摘录:
func main() {
quit := make(chan int)
readyQueue := make(chan Proc)
runQueue := make(chan Proc)
waitQueue := make(chan Proc)
procList := getInitialProcList()
fmt.Println(procList)
for _, proc := range(procList) {
switch {
case proc.Status == READY:
readyQueue <- proc
tick(quit, readyQueue, runQueue, waitQueue)
case proc.Status == RUN:
runQueue <- proc
tick(quit, readyQueue, runQueue, waitQueue)
case proc.Status == WAIT:
waitQueue <- proc
tick(quit, readyQueue, runQueue, waitQueue)
}
}
<-quit // blocks to keep main thread alive
}
func tick(quit chan int, readyQueue chan Proc, runQueue chan Proc, waitQueue chan Proc) {
select {
case p := <-readyQueue:
fmt.Println(p)
default:
fmt.Println("[tick] nothing in ready queue")
}
select {
case p := <-waitQueue:
fmt.Println(p)
default:
fmt.Println("[tick] nothing in wait queue")
}
select {
case p := <-runQueue:
fmt.Println(p)
default:
fmt.Println("[tick] nothing in run queue")
}
quit <- 0
}
我不明白为什么我在上面代码中的readyQueue <- proc 行上收到错误fatal error: all goroutines are asleep - deadlock!。
【问题讨论】:
-
不使用 goroutine 的渠道是什么?