为什么在使用 WaitGroups 和 Buffered Channels 的 Go 代码中会出现死锁?
来源:stackoverflow
时间:2024-03-07 16:33:23 452浏览 收藏
本篇文章给大家分享《为什么在使用 WaitGroups 和 Buffered Channels 的 Go 代码中会出现死锁?》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。
等待组、缓冲通道和死锁
我的这段代码会导致死锁,但我不确定为什么。我尝试在几个不同的地方使用互斥锁,关闭单独的 go 例程内外的通道,但结果仍然相同。
我尝试通过一个通道 (inputchan) 发送数据,然后从另一个通道 (outputchan) 读取数据
package main import ( "fmt" "sync" ) func listStuff(wg *sync.WaitGroup, workerID int, inputChan chan int, outputChan chan int) { defer wg.Done() for i := range inputChan { fmt.Println("sending ", i) outputChan <- i } } func List(workers int) ([]int, error) { _output := make([]int, 0) inputChan := make(chan int, 1000) outputChan := make(chan int, 1000) var wg sync.WaitGroup wg.Add(workers) fmt.Printf("+++ Spinning up %v workers\n", workers) for i := 0; i < workers; i++ { go listStuff(&wg, i, inputChan, outputChan) } for i := 0; i < 3000; i++ { inputChan <- i } done := make(chan struct{}) go func() { close(done) close(inputChan) close(outputChan) wg.Wait() }() for o := range outputChan { fmt.Println("reading from channel...") _output = append(_output, o) } <-done fmt.Printf("+++ output len: %v\n", len(_output)) return _output, nil } func main() { List(5) }
正确答案
主函数中的代码是连续的,首先尝试将 3k 值写入 inputchan
然后将从 outputchan
读取值。
您的代码会在第一个步骤中阻塞:
- 在 3k 值成功发送到
inputchan
之前,outputchan
不会流失任何内容,因此工作人员最终会在第一个 1k 值之后卡在outputchan <- i
- 一旦工作人员停止从
inputchan
中消耗资源,main
将在大约 2k 个值之后卡在inputchan <- i
解决此问题的一种方法是让生产者 (inputchan <- i
) 和最终消费者 (for o := range outputchan {
) 在单独的 goroutine 中运行。
您可以将这些演员之一保留在主 goroutine 中,并为另一个演员旋转一个新演员。例如:
go func(inputchan chan<- int){ for i := 0; i < 3000; i++ { inputchan <- i } close(inputchan) }(inputchan) done := make(chan struct{}) go func() { close(done) // close(inputchan) // i chose to close inputchan above, don't close it twice close(outputchan) wg.wait() }() ...
https://go.dev/play/p/dobgfkabyao
一个额外的注意事项:围绕发信号 done
的操作顺序很重要;通道 done
和 outputchan
只能在 wg.done()
指示所有工作人员完成后关闭
// it is best to close inputChan next to the code that controls // when its input is complete. close(inputChan) // If you had several producers writing to the same channel, you // would probably have to add a separate waitgroup to handle closing, // much like you did for your workers go func() { wg.Wait() // the two following actions must happen *after* workers have // completed close(done) close(outputChan) }()
好了,本文到此结束,带大家了解了《为什么在使用 WaitGroups 和 Buffered Channels 的 Go 代码中会出现死锁?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习