在继续之前要求 Go 运行所有 goroutine
在进行并发编程时,我们经常遇到需要等待所有goroutine完成后再继续执行的情况。在Go语言中,我们可以通过使用WaitGroup来实现这个目标。WaitGroup是一个计数信号量,可以用于等待一组goroutine的完成。在继续之前,我们需要调用WaitGroup的Wait方法,这样可以确保所有的goroutine都已经完成了任务。在本文中,我们将介绍如何正确使用WaitGroup来管理goroutine的执行顺序。
问题内容
我需要 golang 调度程序在继续之前运行所有 goroutine,runtime.gosched() 无法解决。
问题在于 go 例程运行速度太快,以至于 start() 中的“select”在 stopstream() 内的“select”之后运行,然后“case <-chanstopstream:”接收器尚未准备好发件人“case retchan <-true:”。 结果是,当发生这种情况时,结果与 stopstream() 挂起时的行为相同
运行此代码https://go.dev/play/p/dq85xqju2q_z 很多时候你会看到这两个回应 未挂起时的预期响应:
2009/11/10 23:00:00 start 2009/11/10 23:00:00 receive chan 2009/11/10 23:00:03 end
挂起时的预期响应,但挂起时的响应却不是那么快:
2009/11/10 23:00:00 start 2009/11/10 23:00:00 default 2009/11/10 23:00:01 timer 2009/11/10 23:00:04 end
代码
package main import ( "log" "runtime" "sync" "time" ) var wg sync.WaitGroup func main() { wg.Add(1) //run multiples routines on a huge system go start() wg.Wait() } func start() { log.Println("Start") chanStopStream := make(chan bool) go stopStream(chanStopStream) select { case <-chanStopStream: log.Println("receive chan") case <-time.After(time.Second): //if stopStream hangs do not wait more than 1 second log.Println("TIMER") //call some crash alert } time.Sleep(3 * time.Second) log.Println("end") wg.Done() } func stopStream(retChan chan bool) { //do some work that can be faster then caller or not runtime.Gosched() //time.Sleep(time.Microsecond) //better solution then runtime.Gosched() //time.Sleep(2 * time.Second) //simulate when this routine hangs more than 1 second select { case retChan <- true: default: //select/default is used because if the caller times out this routine will hangs forever log.Println("default") } }
解决方法
在继续执行当前 goroutine 之前,没有办法运行所有其他 goroutine。
通过确保 goroutine 不会在 stopstream
上阻塞来修复问题:
选项 1:将 chanstopstream
更改为缓冲通道chanstopstream
更改为缓冲通道。这确保了 stopstream
。这确保了
func start() { log.println("start") chanstopstream := make(chan bool, 1) // <--- buffered channel go stopstream(chanstopstream) ... } func stopstream(retchan chan bool) { ... // always send. no select/default needed. retchan <- true }
选项 2
:关闭通道而不是发送值。通道始终可以由发送者关闭。在关闭的通道上接收返回通道值类型的零值。
func start() { log.Println("Start") chanStopStream := make(chan bool) // buffered channel not required go stopStream(chanStopStream) ... func stopStream(retChan chan bool) { ... close(retChan) }
以上是在继续之前要求 Go 运行所有 goroutine的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Go语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

Go语言中字符串打印的区别:使用Println与string()函数的效果差异在Go...

GoLand中自定义结构体标签不显示怎么办?在使用GoLand进行Go语言开发时,很多开发者会遇到自定义结构体标签在�...

Go语言中哪些库是大公司开发或知名开源项目?在使用Go语言进行编程时,开发者常常会遇到一些常见的需求,�...

Go语言中使用RedisStream实现消息队列时类型转换问题在使用Go语言与Redis...

Go语言中结构体定义的两种方式:var与type关键字的差异Go语言在定义结构体时,经常会看到两种不同的写法:一�...

Go指针语法及viper库使用中的寻址问题在使用Go语言进行编程时,理解指针的语法和使用方法至关重要,尤其是在...
