Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Go_goroutine 版本 goroutine 同步问题 #122

Closed
hmgle opened this issue Jul 14, 2017 · 1 comment
Closed

[bug] Go_goroutine 版本 goroutine 同步问题 #122

hmgle opened this issue Jul 14, 2017 · 1 comment

Comments

@hmgle
Copy link
Contributor

hmgle commented Jul 14, 2017

GetInfos() 函数返回前并没有等待 info_ch 里面的数据处理完:

	go func(info_ch chan *Info, list *[]Info) {
		for {
			tmp := <-info_ch
			if tmp != nil {
				*list = append(*list, *tmp)
			}
		}
	}(info_ch, &list)

我运行了该程序几次,大部分时候的结果都是漏加了数据。把上面的代码 sleep 一下,就 100% 重现了:

	go func(info_ch chan *Info, list *[]Info) {
		for {
			tmp := <-info_ch
			time.Sleep(time.Millisecond * 10)
			if tmp != nil {
				*list = append(*list, *tmp)
			}
		}
	}(info_ch, &list)

正确的做法要保证这个 goroutine 处理完数据该函数才返回。

@lilydjwg
Copy link
Owner

谢谢。这个重现率不是很高,以至于一直都没有发现……

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants