为了账号安全,请及时绑定邮箱和手机立即绑定

Golang 模式一次杀死多个 goroutine

Golang 模式一次杀死多个 goroutine

Go
守候你守候我 2022-05-23 17:26:30
我有两个 goroutine,如下面的代码片段所示。我想同步它们,这样当一个返回时,另一个也应该退出。实现这一目标的最佳方法是什么?func main() {  go func() {    ...    if err != nil {      return    }  }()  go func() {    ...    if err != nil {      return    }  }()}我在这里模拟了这个场景https://play.golang.org/p/IqawStXt7rt并试图用一个通道来解决它,以表示一个例程已经完成。这看起来可能会写入已关闭的通道,从而导致恐慌。解决此问题的最佳方法是什么?
查看完整描述

3 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

您可以使用上下文在两个 go 例程之间进行通信。例如,


package main


import (

    "context"

    "sync"

)


func main() {


    ctx, cancel := context.WithCancel(context.Background())

    wg := sync.WaitGroup{}

    wg.Add(3)

    go func() {

        defer wg.Done()

        for {

            select {

            // msg from other goroutine finish

            case <-ctx.Done():

                // end

            }

        }

    }()


    go func() {

        defer wg.Done()

        for {

            select {

            // msg from other goroutine finish

            case <-ctx.Done():

                // end

            }

        }

    }()


    go func() {

        defer wg.Done()

        // your operation

        // call cancel when this goroutine ends

        cancel()

    }()

    wg.Wait()

}


查看完整回答
反对 回复 2022-05-23
?
慕田峪4524236

TA贡献1875条经验 获得超5个赞

在通道上使用 close 表示完成。这允许多个 goroutine 通过在通道上接收来检查完成情况。


每个 goroutine 使用一个通道来表示 goroutine 的完成。


done1 := make(chan struct{}) // closed when goroutine 1 returns

done2 := make(chan struct{}) // closed when goroutine 2 returns


go func() {

    defer close(done1)


    timer1 := time.NewTicker(1 * time.Second)

    defer timer1.Stop()


    timer2 := time.NewTicker(2 * time.Second)

    defer timer2.Stop()


    for {

        select {

        case <-done2:

            // The other goroutine returned.

            fmt.Println("done func 1")

            return

        case <-timer1.C:

            fmt.Println("timer1 func 1")

        case <-timer2.C:

            fmt.Println("timer2 func 1")

            return

        }


    }

}()


go func() {

    defer close(done2)

    for {

        select {

        case <-done1:

            // The other goroutine returned.

            fmt.Println("done func 2")

            return

        default:

            time.Sleep(3 * time.Second)

            fmt.Println("sleep done from func 2")

            return

        }


    }

}()


fmt.Println("waiting for goroutines to complete")


// Wait for both goroutines to return. The order that

// we wait here does not matter. 

<-done1

<-done2


fmt.Println("all done")


查看完整回答
反对 回复 2022-05-23
?
喵喵时光机

TA贡献1846条经验 获得超7个赞

首先将等待 go-routines 和donechannel 分开。


使用 async.WaitGroup来协调 goroutine。


func main() {

    wait := &sync.WaitGroup{}

    N := 3


    wait.Add(N)

    for i := 1; i <= N; i++ {

        go goFunc(wait, i, true)

    }


    wait.Wait()

    fmt.Println(`Exiting main`)

}

每个 goroutine 将如下所示:


// code for the actual goroutine

func goFunc(wait *sync.WaitGroup, i int, closer bool) {

    defer wait.Done()

    defer fmt.Println(`Exiting `, i)


    T := time.Tick(time.Duration(100*i) * time.Millisecond)

    for {

        select {

        case <-T:

            fmt.Println(`Tick `, i)

            if closer {

                return

            }

        }

    }

}

(https://play.golang.org/p/mDO4P56lzBU)


我们的 main 函数在退出之前成功地等待 goroutines 退出。每个 goroutine 都在关闭自己,我们想要一种同时取消所有 goroutine 的方法。


我们将使用chan, 并利用从频道接收的这一特性:


引用:关闭通道上的接收操作总是可以立即进行,在接收到任何先前发送的值之后产生元素类型的零值。(https://golang.org/ref/spec#Receive_operator)


我们修改我们的 goroutine 来检查 CLOSE:


func goFunc(wait *sync.WaitGroup, i int, closer bool, CLOSE chan struct{}) {

    defer wait.Done()

    defer fmt.Println(`Exiting `, i)


    T := time.Tick(time.Duration(100*i) * time.Millisecond)

    for {

        select {

        case <-CLOSE:

            return

        case <-T:

            fmt.Println(`Tick `, i)

            if closer {

                close(CLOSE)

            }

        }

    }

}

然后我们改变我们的func main,让它通过 CLOSE 通道,我们将设置closer变量,以便只有我们的最后一个 goroutine 会触发关闭:


func main() {

    wait := &sync.WaitGroup{}

    N := 3

    CLOSE := make(chan struct{})


    // Launch the goroutines

    wait.Add(N)

    for i := 1; i <= N; i++ {

        go goFunc(wait, i, i == N, CLOSE)

    }


    // Wait for the goroutines to finish

    wait.Wait()

    fmt.Println(`Exiting main`)

}

(https://play.golang.org/p/E91CtRAHDp2)


现在看起来一切正常。


但事实并非如此。并发很难。这段代码中潜伏着一个错误,正等着在生产中咬你。让我们浮出水面。


更改我们的示例,以便每个goroutine 都将关闭:


func main() {

    wait := &sync.WaitGroup{}

    N := 3

    CLOSE := make(chan struct{})


    // Launch the goroutines

    wait.Add(N)

    for i := 1; i <= N; i++ {

        go goFunc(wait, i, true /*** EVERY GOROUTINE WILL CLOSE ***/, CLOSE)

    }


    // Wait for the goroutines to finish

    wait.Wait()

    fmt.Println(`Exiting main`)

}

更改 goroutine 以便在关闭之前需要一段时间。我们希望两个 goroutine 同时关闭:


// code for the actual goroutine

func goFunc(wait *sync.WaitGroup, i int, closer bool, CLOSE chan struct{}) {

    defer wait.Done()

    defer fmt.Println(`Exiting `, i)


    T := time.Tick(time.Duration(100*i) * time.Millisecond)

    for {

        select {

        case <-CLOSE:

            return

        case <-T:

            fmt.Println(`Tick `, i)

            if closer {

                /*** TAKE A WHILE BEFORE CLOSING ***/

                time.Sleep(time.Second)

                close(CLOSE)

            }

        }

    }

}



(https://play.golang.org/p/YHnbDpnJCks)


我们崩溃:


Tick  1

Tick  2

Tick  3

Exiting  1

Exiting  2

panic: close of closed channel


goroutine 7 [running]:

main.goFunc(0x40e020, 0x2, 0x68601, 0x430080)

    /tmp/sandbox558886627/prog.go:24 +0x2e0

created by main.main

    /tmp/sandbox558886627/prog.go:38 +0xc0


Program exited: status 2.

虽然关闭通道上的接收立即返回,但您无法关闭关闭的通道。


我们需要一点协调。我们可以用 async.Mutex和 abool来表示我们是否关闭了通道。让我们创建一个结构来执行此操作:


type Close struct {

    C chan struct{}

    l sync.Mutex

    closed bool

}


func NewClose() *Close {

    return &Close {

        C: make(chan struct{}),

    }

}


func (c *Close) Close() {

    c.l.Lock()

    if (!c.closed) {

        c.closed=true

        close(c.C)

    }

    c.l.Unlock()

}

重写我们的 gofunc 和我们的 main 以使用我们新的 Close 结构,我们很高兴: https: //play.golang.org/p/eH3djHu8EXW


并发的问题在于,您总是需要想知道如果另一个“线程”在代码中的其他任何地方会发生什么。


查看完整回答
反对 回复 2022-05-23
  • 3 回答
  • 0 关注
  • 190 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号