package main
import (
"fmt"
"runtime"
"sync"
"time"
)
func main() {
intInputChan := make(chan int, 50)
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go worker(intInputChan, wg)
}
for i := 1; i < 51; i++ {
fmt.Printf("Inputs. %d \n", i)
intInputChan <- i
}
close(intInputChan)
wg.Wait()
fmt.Println("Existing Main App... ")
panic("---------------")
}
func worker(input chan int, wg sync.WaitGroup) {
defer func() {
fmt.Println("Executing defer..")
wg.Done()
}()
for {
select {
case intVal, ok := <-input:
time.Sleep(100 * time.Millisecond)
if !ok {
input = nil
return
}
fmt.Printf("%d %v\n", intVal, ok)
default:
runtime.Gosched()
}
}
}
拋出的錯誤是。爲什麼所有的goroutines都睡着了 - 僵局。識別瓶頸
致命錯誤:所有goroutines都睡着了 - 僵局!
夠程1 [semacquire]: 同步(* WaitGroup).Wait(0xc082004600) C:/go/src/sync/waitgroup.go:132 +量0x170 main.main() E:/圍棋/go_projects/go/src/Test.go:22 + 0x21a
而不是寫'無功WG的sync.WaitGroup''寫WG:=新(sync.WaitGroup)',你永遠不會再次犯這個錯誤! – 2015-02-24 09:49:54
-1:[有些問題仍然存在問題,即使它們符合上面列出的其中一個類別:尋求調試幫助的問題(「爲什麼代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現它所需的最短代碼。沒有明確問題陳述的問題對其他讀者沒有用處](http://stackoverflow.com/help/on-topic) – Vector 2015-04-23 21:30:17