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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看