登录
首页 >  Golang >  Go问答

在select语句中,应该先使用channel的buffer还是先使用pending队列中的值?

来源:stackoverflow

时间:2024-02-23 15:18:23 248浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《在select语句中,应该先使用channel的buffer还是先使用pending队列中的值?》,聊聊,我们一起来看看吧!

问题内容

最近在读go中select的源码。代码的某些部分我不明白。

例如,当选择的通道有待处理的发送操作或待处理的接收操作时,它首先使用待处理的发送中的值而不是通道缓冲区中的值。

// pass 1 - look for something already waiting
    var dfli int
    var dfl *scase
    var casi int
    var cas *scase
    var recvok bool
    for i := 0; i < ncases; i++ {
        casi = int(pollorder[i])
        cas = &scases[casi]
        c = cas.c

        switch cas.kind {
        case casenil:
            continue

        case caserecv:
            sg = c.sendq.dequeue()
            if sg != nil {
                goto recv
            }
            if c.qcount > 0 {
                goto bufrecv
            }
            if c.closed != 0 {
                goto rclose
            }

        case casesend:
            if raceenabled {
                racereadpc(c.raceaddr(), cas.pc, chansendpc)
            }
            if c.closed != 0 {
                goto sclose
            }
            sg = c.recvq.dequeue()
            if sg != nil {
                goto send
            }
            if c.qcount < c.dataqsiz {
                goto bufsend
            }

        case casedefault:
            dfli = casi
            dfl = cas
        }
    }

所以我写了一些简单的代码来验证我的假设。但结果表明我错了。

package main

import (
    "fmt"
    "time"
)

func main() {
    var mark chan int
    length := 3
    c := make(chan int, length)
    for i := 1; i <= length; i++ {
        c <- i
    }

    go func() {
        c <- 4
    }()

    time.Sleep(time.Millisecond * 10)
    select {
    case val := <-c:
        fmt.Println("receive val is ", val)
    case _ = <-mark:
        fmt.Println("block chan")
    }
}

结果是 receive val 是 1。但是,我认为打印输出应该是 receive val is 4。看来 select 首先使用缓冲区中的值。


正确答案


你读错了源代码。

代码 goto recv 转到标记为 recv 的代码块,即:

recv:
    // can receive from sleeping sender (sg)
    recv(c, sg, cas.elem, func() { selunlock(scases, lockorder) }, 2)
    if debugselect {
        print("syncrecv: cas0=", cas0, " c=", c, "\n")
    }
    recvok = true
    goto retc

如果您查看在 channel.go 中定义的 recv 函数 (https://golang.org/src/runtime/chan.go#L607),您会看到它从缓冲区中读取(如果有的话)。从该函数的文档中:

// recv processes a receive operation on a full channel c.
// There are 2 parts:
// 1) The value sent by the sender sg is put into the channel
//    and the sender is woken up to go on its merry way.
// 2) The value received by the receiver (the current G) is
//    written to ep.
// For synchronous channels, both values are the same.
// For asynchronous channels, the receiver gets its data from
// the channel buffer and the sender's data is put in the
// channel buffer.
// Channel c must be full and locked. recv unlocks c with unlockf.
// sg must already be dequeued from c.
// A non-nil ep must point to the heap or the caller's stack.

总而言之,所讨论的情况只是看看是否有发送者在等待它,这意味着要么由于通道没有缓冲,要么通道的缓冲区已满;它与接收者接收到的值无关。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《在select语句中,应该先使用channel的buffer还是先使用pending队列中的值?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>