goroutine睡着了 - 僵局

时间:2016-11-04 13:06:14

标签: go deadlock goroutine

在下面的代码中,我试图生成MaxOutstanding个处理程序。每个处理程序循环遍历队列queue中的项目并将其打印出来,我还将true写入done频道。

在我的main函数中,我启动处理程序并将9个元素写入queue并等待第一个元素写入done队列。

package main


import "fmt"

type Request struct {
        int32
}
var MaxOutstanding = 5
func handle(queue chan *Request, i int, done chan bool) {
    for r := range queue {
        fmt.Println(i, "---", r)
        done <- true
    }
}

func Serve(clientRequests chan *Request, quit, done chan bool) {
    // Start handlers
    for i := 0; i < MaxOutstanding; i++ {
        go handle(clientRequests, i, done)
    }
    <-quit  // Wait to be told to exit.
}


func main() {
    clientRequests := make(chan *Request)
    quit := make(chan bool)
    done := make(chan bool)

    go Serve(clientRequests, quit, done)

    clientRequests <- &Request{4}
    clientRequests <- &Request{1}
    clientRequests <- &Request{2}
    clientRequests <- &Request{3}

    clientRequests <- &Request{5}
    clientRequests <- &Request{6}
    clientRequests <- &Request{7}
    clientRequests <- &Request{8}
    clientRequests <- &Request{9}
    fmt.Println( "...........>", <- done )
    close(clientRequests)
    close(done)
}

执行时出现以下错误。我没有看到实施的错误,我甚至关闭了频道。

4 --- &{4}
0 --- &{1}
1 --- &{2}
2 --- &{3}
3 --- &{5}
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send]:
main.main()
        /home/ubuntu/digs-svc/src/digs/go1.go:45 +0x251

goroutine 5 [chan receive]:
main.Serve(0xc82004c060, 0xc82004c0c0, 0xc82004c120)
        /home/ubuntu/digs-svc/src/digs/go1.go:28 +0x92
created by main.main
        /home/ubuntu/digs-svc/src/digs/go1.go:37 +0xb9

goroutine 6 [chan send]:
main.handle(0xc82004c060, 0x0, 0xc82004c120)
        /home/ubuntu/digs-svc/src/digs/go1.go:16 +0x23b
created by main.Serve
        /home/ubuntu/digs-svc/src/digs/go1.go:25 +0x5b

编辑:

显然,fmt.Println("....", <- done)并不足以表明done频道有消费者。我按执行顺序移动了代码。消费者需要&#34;倾听&#34;在数据写入通道时在通道上。在我之前的代码中,第一个数据写入的时间没有消费者。

工作代码。

https://play.golang.org/p/98l2M4XO9t

2 个答案:

答案 0 :(得分:1)

您通过done频道上的发送阻止了处理函数中频道的迭代,因为另一方没有收到任何内容。

这些额外的渠道并没有真正做任何事情,你可以添加一个WaitGroup来同步处理程序的退出,然后你可以删除done通道,这将允许处理程序继续。

func handle(queue chan *Request, i int, wg *sync.WaitGroup) {
    defer wg.Done()

    for r := range queue {
        fmt.Println(i, "---", r)
    }
}

func Serve(clientRequests chan *Request, wg *sync.WaitGroup) {
    // Start handlers
    for i := 0; i < MaxOutstanding; i++ {
        wg.Add(1)
        go handle(clientRequests, i, wg)

    }
}

func main() {
    clientRequests := make(chan *Request)
    var wg sync.WaitGroup

    go Serve(clientRequests, &wg)

    for i := int32(0); i < 50; i++ {
        clientRequests <- &Request{i}
    }

    close(clientRequests)
    wg.Wait()
}

https://play.golang.org/p/oUFjZONjhk(请注意,在操场上,这个示例似乎目前支持单个goroutine作为接收器。通常,被阻止的goroutine将随机接收,如果编译并正常运行,您可以看到该行为)< / p>

答案 1 :(得分:1)

在for循环中,您只将通道操作处理到第5个元素,但是在主函数中,您尝试将值通过该值发送到通道,该通道已关闭。

要克服这种情况,您可以在for循环中发送请求值:

for i := 0; i < MaxOutstanding; i++ {
        clientRequests <- &Request{int32(i)}
}

以下是工作代码:

package main

import (
    "fmt"
)

type Request struct {
    int32
}

var MaxOutstanding = 10

func handle(queue chan *Request, i int, done chan bool) {
    for r := range queue {
        fmt.Println(i, "---", r)
        done <- true
    }
}

func Serve(clientRequests chan *Request, quit, done chan bool) {
    // Start handlers
    for i := 0; i < MaxOutstanding; i++ {
        go handle(clientRequests, i, done)
    }
    <-quit // Wait to be told to exit.
}

func main() {
    clientRequests := make(chan *Request)
    quit := make(chan bool)
    done := make(chan bool)

    go Serve(clientRequests, quit, done)
    for i := 0; i < MaxOutstanding; i++ {
        clientRequests <- &Request{int32(i)}
    }

    fmt.Println("...........>", <-done)
    close(clientRequests)
    close(done)
}

https://play.golang.org/p/L5Y2YoFNvz