Golang并发同步问题

huangapple go评论79阅读模式
英文:

golang concurrency sync issue

问题

我在使用goroutines时遇到了同步问题。我的程序输出的结果是不可预测的。我查看了文档,对于无缓冲通道,没有办法检查是否所有的消息都已经被处理。我将问题简化为了这个小的演示代码,仍然展示了这个问题。显然,这不是Golang的问题,而是我的代码的问题。显然,我没有使用正确的并发模式。

问题是如何解决这个问题。如果可能的话,我既不想关闭通道,也不想停止hive goroutine。我认为如果我可以假设一旦所有的bee goroutine都完成了,那么hive现在也完成了(这就是我尝试使用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)
}

每隔一段时间,程序在完成之前就会打印出结果。

$ 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
英文:

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(
	&quot;fmt&quot;
	&quot;sync&quot;
	&quot;time&quot;
)



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

	// bees
	for i:=0; i&lt;5000;i++{
		wg.Add(1)
		go func(in chan int64) {
			defer wg.Done()
			time.Sleep(100)
			in &lt;- 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

得分: 4

你从来没有等待“hive”循环完成,所以有时你在完成之前就打印了count的值。

最简单的方法是使用WaitGroup来在关闭通道时发出信号,并在for range循环中阻塞main

go func() {
    wg.Wait()
    close(c)
}()

for out := range c {
    count += out
}

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

英文:

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

huangapple
  • 本文由 发表于 2015年12月28日 23:22:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/34496149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定