doujingya1166 2014-12-15 23:12
浏览 47
已采纳

使用指针变量转到Channel和go例程,所有goroutine都处于睡眠状态-死锁

I spend my evening looking at how to fix this error but I haven't succeeded. When I run the program I have the following error : "all goroutines are asleep - deadlock!". I understand this is because the main program is exited before the routine has the possibility to do its tasks and I thought using sync.WaitGroup would help but not really :/

I want to set a number of routine and use channels to send urls in order to check http status code. I want to limit the number of concurrency call to a website. I've followed examples doing the same thing with string instead of struct and it worked.

Any help would be well appreciated :)

package main

import (
    "fmt"
    "sync"
    "time"
)

const (
    numPollers = 2                // number of Poller goroutines to launch
)

var urls = []string{
    "http://www.google.com/",
    "http://golang.org/",
    "http://blog.golang.org/",
    "http://golangtutorials.blogspot.fr",
    "https://gobyexample.com/",
}

// Resource represents an HTTP URL to be polled by this program.
type Resource struct {
    url      string
}

func Poller(in <-chan *Resource, wg *sync.WaitGroup) {
    //defer wg.Done()
    for r := range in {
        fmt.Printf("Finished: %v - %v
", r.url, time.Now())
    }
    wg.Done()
}

func main() {
    var wg sync.WaitGroup
    pending := make(chan *Resource)

    wg.Add(len(urls))

    go Poller(pending, &wg)

    go func() {
        for _, url := range urls {
            wg.Add(1)
            fmt.Println("SENT > Pending url " + url)
            pending <- &Resource{url: url}
        }
    }()

    wg.Wait()

    fmt.Printf("Finished all goroutines: %v
", time.Now())
}

https://play.golang.org/p/B-HSiDo2Qg

  • 写回答

2条回答 默认 最新

  • dp152153 2014-12-15 23:30
    关注

    First, you have too many calls to wg.Add(). You call that once for each goroutine you're running. See http://golang.org/pkg/sync/#WaitGroup. Second, you didn't close the channel after you were done writing to it. Here's a modified version of your code:

    package main
    
    import (
        "fmt"
        "sync"
        "time"
    )
    
    const (
        numPollers = 2                // number of Poller goroutines to launch
    )
    
    var urls = []string{
        "http://www.google.com/",
        "http://golang.org/",
        "http://blog.golang.org/",
        "http://golangtutorials.blogspot.fr",
        "https://gobyexample.com/",
    }
    
    // Resource represents an HTTP URL to be polled by this program.
    type Resource struct {
        url      string
    }
    
    func Poller(in <-chan *Resource, wg *sync.WaitGroup) {
        defer wg.Done()
        for r := range in {
            fmt.Printf("Finished: %v - %v
    ", r.url, time.Now())
        }
    }
    
    func main() {
        var wg sync.WaitGroup
        pending := make(chan *Resource)
    
        wg.Add(2)
    
        go Poller(pending, &wg)
    
        go func() {
            defer close(pending)
            defer wg.Done()
            for _, url := range urls {
                fmt.Println("SENT > Pending url " + url)
                pending <- &Resource{url: url}
            }
        }()
    
        wg.Wait()
    
        fmt.Printf("Finished all goroutines: %v
    ", time.Now())
    }
    

    and https://play.golang.org/p/ucUlZEZMZM

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序