duandu5846 2019-04-05 04:41
浏览 79
已采纳

无论是否关闭频道,缩小地图都无法正常工作

This is homework and beginner question. I edited the question since I made a mistake that I found.

I am trying to do parallel frequency maps from a text and I get an error in the last operation (reducing maps)

The code seem to work up to this point.

If I close the channel I get error: "panic: send on closed channel"

If I do not close the channel I get: "fatal error: all goroutines are asleep - deadlock!"

func WordCount(text string) {

    text = strings.ToLower(text)
    re := regexp.MustCompile("\\w+")
    sentence := re.FindAllString(text, -1)

    numberOfGroups := 4
    piece := len(sentence) / numberOfGroups

    wordChannel := make(chan map[string]int)

    wg := new(sync.WaitGroup)
    wg.Add(numberOfGroups)

    for i := 0; i < numberOfGroups; i ++ {
        go processToCounting(sentence[i*piece:(i+1)*piece], wordChannel, wg)
    }

    wg.Wait()
    fmt.Print(<-wordChannel)
    fmt.Print("
")

    finalMap := make(map[string]int)
    close(wordChannel)

    for i := 0; i < numberOfGroups; i++ {
        for k, v := range <- wordChannel {
            finalMap[k] += v
        }
    }
}

func processToCounting(textSlice []string, wordChannel chan map[string]int, wg *sync.WaitGroup) {
    freq := make(map[string]int)
    for _, v := range textSlice {
        freq[v]++
    }
    wg.Done()
    wordChannel <- freq
}
  • 写回答

2条回答 默认 最新

  • duanrong0738 2019-04-05 13:58
    关注

    1. First question: panic

    If I close the channel I get error: "panic: send on closed channel"

    Why? One of you goroutines is trying to write to the channel which you already closed in the calling (main) goroutine. In your case in WordCount function.

    In the current version of your code the panic is not reproducible with my test sentence, but you can easily cause this e.g. if you would call close(wordChannel) before wg.Wait().

    Lets look at the Bug in processToCounting which may cause the panic:

    wg.Done() // tells to the WaitGroup that the gouroutine is Done (decrements the counter of goroutines)
    wordChannel <- freq // trying to write to the channel
    

    Here wg.Done() signals to the WaitGroup that the goroutine is Done before actual writing to the channel has happened. The calling goroutine (WordCount function) at some point thinks that all gouroutines are done (wg.Wait() line) and closes the channel. But one of your goroutine which have not finished writing, will try to write to the closed channel. Then you will get the panic.

    How to fix:

    Use defer in processToCounting function

    defer wg.Done() // triggers wg.Done right before the function returns (but after writing to the channel in your case) 
    

    What to read:

    See for beginners in A Tour of Go / Concurrency

    Sending on a closed channel will cause a panic.

    and documentation for: close

    Sending to or closing a closed channel causes a run-time panic.

    2. Second question: deadlock

    If I do not close the channel I get: "fatal error: all goroutines are asleep - deadlock!"

    You have a for-loop which reads from the channel. This for-loop is locked forever. Waiting for new values from the channel, but nobody will write there anymore.

    See in A Tour of Go / Concurrency

    The loop for i := range c receives values from the channel repeatedly until it is closed.

    and see documentation for Channels

    Receivers always block until there is data to receive

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog