2012-11-26 14 views
7
package main 

import (
    "fmt" 
    "sync" 
) 

func push(c chan int,wg sync.WaitGroup) { 
    for i := 0; i < 5; i++ { 
     c <- i 
    } 
    wg.Done() 
} 

func pull(c chan int,wg sync.WaitGroup) { 
    for i := 0; i < 5; i++ { 
     result,ok := <- c 
     fmt.Println(result,ok) 
    } 
    wg.Done() 
} 

func main() { 
    var wg sync.WaitGroup 
    wg.Add(2) 
    c := make(chan int) 

    go push(c,wg) 
    go pull(c,wg) 

    wg.Wait() 
} 

आउटपुट:मेरा गोलांग चैनल मृत लॉक त्रुटि क्यों बढ़ाता है?

localhost:src kuankuan$ go run goroutine.go 
0 true 
1 true 
2 true 
3 true 
4 true 
throw: all goroutines are asleep - deadlock! 

goroutine 1 [semacquire]: 
sync.runtime_Semacquire(0x42130100, 0x42130100) 
    /usr/local/go/src/pkg/runtime/zsema_amd64.c:146 +0x25 
sync.(*WaitGroup).Wait(0x42120420, 0x0) 
    /usr/local/go/src/pkg/sync/waitgroup.go:79 +0xf2 
main.main() 
    /Users/kuankuan/go/src/goroutine.go:31 +0xb9 

goroutine 2 [syscall]: 
created by runtime.main 
    /usr/local/go/src/pkg/runtime/proc.c:221 
exit status 2 

उत्तर

17

कारण है कि यह गतिरोध क्योंकि structs संदर्भ द्वारा मूल्य द्वारा पारित कर रहे हैं और नहीं है।

जब आप अपने कार्यों में प्रतीक्षा समूह को पास करते हैं, तो आपको पॉइंटर पास करने की आवश्यकता नहीं है और मूल्य नहीं है। अन्यथा प्रतीक्षा समूह की एक प्रति का उपयोग किया जाएगा।

यह आपके काम कर उदाहरण है:

package main 

import (
    "fmt" 
    "sync" 
) 

func push(c chan int,wg *sync.WaitGroup) { 
    for i := 0; i < 5; i++ { 
     c <- i 
    } 
    wg.Done() 
} 

func pull(c chan int,wg *sync.WaitGroup) { 
    for i := 0; i < 5; i++ { 
     result,ok := <- c 
     fmt.Println(result,ok) 
    } 
    wg.Done() 
} 

func main() { 
    var wg sync.WaitGroup 
    wg.Add(2) 
    c := make(chan int) 

    go push(c,&wg) 
    go pull(c,&wg) 

    wg.Wait() 
} 
संबंधित मुद्दे