周末刚开始使用 Go,我不确定我是否正确使用了 Go 的特性,或者我是否根本没有做过这种“类似 Go”的工作。该代码应在称为的地图元素上进行迭代non_placed_alleles,并将每个元素与中的所有元素进行比较,这些元素placed_alleles也存储在地图中。我正在尝试对每个元素使用一个go-routine,non_placed_alleles因为比较成本很高,而且要花很长时间。这是主函数的一些内容:runtime.GOMAXPROCS(8) // For 8 consecutive routines at once? got 10 CPUsc := 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这是被调用的函数: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}有什么办法可以使这个“更好”?快点?我是否正确使用了指针魔术和 goroutines?
1 回答
- 1 回答
- 0 关注
- 168 浏览
添加回答
举报
0/150
提交
取消