dryk50495 2013-02-11 10:57
浏览 16
已采纳

比较2张地图的元素-我这样做对吗?

just started working with Go over the weekend and I'm unsure whether I used Go's peculiarities right, or whether I haven't done this "Go-like" at all.

The code is supposed to iterate over elements of a map called non_placed_alleles and compare each of these with all elements in placed_alleles, which are stored in a map as well. I'm trying to use one go-routine for each of the elements in non_placed_alleles as the comparison is quite costly and takes forever.

Here's a bit from the main-function:

runtime.GOMAXPROCS(8) // For 8 consecutive routines at once? got 10 CPUs
c := make(chan string)
for name, alleles := range non_placed_alleles {
    go get_best_places(name, alleles, &placed_alleles, c) 
    // pointer to placed_alleles as we only read, not write - should be safe?
}
for channel_item := range c {
    fmt.Println("This came back ", channel_item)
} 
// This also crashes with "all goroutines are sleeping", 
// but all results are printed

And here's the called function:

func get_best_places(name string, alleles []string, placed_alleles *map[string] []string, c chan string) {
    var best_partner string
    // Iterate over all elements of placed_alleles, find best "partner"
    for other_key, other_value := range *placed_alleles {
        best_partner := compare_magic() // omitted because boring
    }
    c <- best_partner
}

Is there any way to make this "better"? Faster? Have I used the pointer-magic and goroutines correctly?

  • 写回答

1条回答 默认 最新

  • dsfliu1129 2013-02-11 11:10
    关注

    Some observations:

    • You probably want to use a bufered channel

      c := make(chan string, someNumber) // someNumber >= goroutines cca

    • The deadlock stems from ranging a channel which no one closes (that's how the range statement terminates). Machinery to coordinate stuff in Go varies per task. Some would perhaps use in this case a sync.WaitGroup. Please check thoroughly the example code located bellow the preceding link for hints about proper use of WaitGroup. An alternative is some/any other kind/way of counting the started vs finished workers (which WaitGroup encapsulates, but I tend to see it as an overkill).

    • No need to pass pointers to map in this case. Maps in Go have full reference semantics (it's just a pointer under the hood anyway).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?