在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学习网公众号了解相关技术文章。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习