首页  >  文章  >  后端开发  >  可以在Go中同时选择发送和接收通道而不阻塞吗?

可以在Go中同时选择发送和接收通道而不阻塞吗?

Susan Sarandon
Susan Sarandon原创
2024-10-26 22:34:31836浏览

 Can You Select on Both Sending and Receiving Channels in Go, Without Blocking?

选择双向通道进行条件发送和接收

在 Go 中,可以为发送和接收设置缓冲通道,允许根据其可用性进行选择性操作。考虑以下场景:

<code class="go">s := make(chan<- int, 5)
r := make(<-chan int)

我们有一个缓冲的发送通道 s 和一个无缓冲的接收通道 r。问题来了:我们能否在两个通道上进行选择来确定 r 是否有数据或 s 是否未满?这模仿了以下功能,但不消耗 100% CPU:

<code class="go">for {
    if len(s) < cap(s) {
        // Send something
    }
    if len(r) > 0 {
        // Receive something
    }
}</code>

使用默认情况实现 Select

要实现这一点,我们可以利用具有默认情况的 select 语句。默认情况下,如果两个通道都没有准备好,我们会暂时“睡眠”goroutine,以避免不必要的资源消耗。

<code class="go">s := make(chan<- int, 5)
r := make(<-chan int)

for {
    v := valueToSend() // Value to be sent, evaluated upon each attempt
    select {
    case s <- v:
        fmt.Println("Sent value:", v)
    case vr := <-r:
        fmt.Println("Received:", vr)
    default: // Neither channel is ready, pause briefly
        time.Sleep(time.Millisecond * 1)
    }
}

为什么长度和容量检查不理想

在尝试发送或接收之前避免检查通道的长度或容量至关重要。这是因为通道的状态可能在检查时间和实际操作之间发生变化,可能导致意外阻塞。例如:

<code class="go">if len(r) > 0 {
    // r is ready to receive

    // Other code...

    r <-  // This may block if another goroutine has already received from r!
}</code>

以上是可以在Go中同时选择发送和接收通道而不阻塞吗?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn