首页 > 后端开发 > Golang > 正文

在继续之前要求 Go 运行所有 goroutine

PHPz
发布: 2024-02-11 17:20:17
转载
898 人浏览过

在继续之前要求 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
}
登录后复制
https://www.php.cn/link/56e48d306028f2a6c2ebf677f7e8f800

选项 2

:关闭通道而不是发送值。通道始终可以由发送者关闭。在关闭的通道上接收返回通道值类型的零值。

func start() {
    log.Println("Start")
    chanStopStream := make(chan bool) // buffered channel not required
    go stopStream(chanStopStream)
    ...

func stopStream(retChan chan bool) {
    ...
    close(retChan)
}
登录后复制
https://www.php.cn/link/a1aa0c486fb1a7ddd47003884e1fc67f🎜

以上是在继续之前要求 Go 运行所有 goroutine的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!