douzheng1853 2015-12-28 15:22
浏览 38
已采纳

golang并发同步问题

I am facing a synchronization issue when using goroutines. My program outputs unpredictable results. I checked the docu and for unbuffered channels there is no way to check if all msgs have been processed. I simplified the issue to this little demo code that still demonstrates the problem. Clearly this is not an issue with Golang but with my code. Obviously I am not using the right concurrency pattern.

Question is how to resolve this. If possible I would neither want to close the channel nor stop the hive goroutine. It think it would be great if I could assume that once all bee goroutines are finished that hive is done for now, too (that is what I tried by using wg.Wait()).

package main

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



func main() {
    count := int64(0)
    c := make(chan int64)
    var wg sync.WaitGroup

    // bees
    for i:=0; i<5000;i++{
        wg.Add(1)
        go func(in chan int64) {
            defer wg.Done()
            time.Sleep(100)
            in <- 2
        }(c)
    }

    // hive
    go func() {
        for out := range c {
            count += out
        }
    }()

    wg.Wait()
    // bang! but why?
    fmt.Println(count)  
}

// every now and again the program prints out before it is finished
// $ go run pattern1.go
// 10000
// $ go run pattern1.go
// 9998
// $ go run pattern1.go
// 9998
// $ go run pattern1.go
// 10000
// $ go run pattern1.go
// 10000
// $ go run pattern1.go
// 9998
  • 写回答

1条回答 默认 最新

  • doujiyan0971 2015-12-28 15:33
    关注

    You're never waiting for the "hive" loop to finish, so sometimes you print the count value before it's complete.

    It's easiest to use the WaitGroup to signal when to close the channel, and block main on the for range loop:

    go func() {
        wg.Wait()
        close(c)
    }()
    
    for out := range c {
        count += out
    }
    

    http://play.golang.org/p/jK24dtG2je

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

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面