合并两个关闭通道

有两个通道将用于通过关闭它们来发送信号。如何从它们中创建一个通道,如果其中至少一个通道关闭,该通道将被关闭。这段代码说明了我想做什么:


func MergeChans(c1 chan struct{}, c2 chan struct{}) chan struct{} {

    c3 := make(chan struct{})

    go func() {

        select {

        case <-c1: close(c3)

        case <-c2: close(c3)

        }

    }()

    return c3

}

是否可以在没有子程序的情况下实现这一目标?


慕无忌1623718
浏览 101回答 1
1回答

白衣染霜花

如果您想在需要关闭之前返回“合并”频道,则不。但这不是问题,您在其中启动的 goroutine 将使用 0 CPU 资源。你不应该为此担心。一旦其中一个通道关闭,该函数将结束,因此 goroutine 将正确终止。您只需要确保至少关闭一个通道即可。如果你不能保证这一点,goroutine 将永远不会终止,也永远不会被垃圾收集。如果您不控制频道,您可以传递第三个频道(或一个context.Context值)以提供一种正确终止的方式,例如:func MergeChans(c1, c2, shutdown chan struct{}) chan struct{} {&nbsp; &nbsp; c3 := make(chan struct{})&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case <-c1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; close(c3)&nbsp; &nbsp; &nbsp; &nbsp; case <-c2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; close(c3)&nbsp; &nbsp; &nbsp; &nbsp; case <-shutdown:&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()&nbsp; &nbsp; return c3}如果你想避免额外的 goroutine,那么不要合并它们(case在需要监控的地方添加 2 s)。
打开App,查看更多内容
随时随地看视频慕课网APP