- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我是围棋菜鸟
我在看这个结构:
for {
select {
case <-resyncCh:
case <-stopCh:
return
case <-cancelCh:
return
}
if r.ShouldResync == nil || r.ShouldResync() {
// do stuff
}
resyncCh = r.resyncChan()
}
for
循环永远运行。break
在 Go 中是隐含的。select
statement are blocking if there's no default
clause (这里没有)。假设 resyncCh
上没有消息。
所有 case
是否并行评估(阻塞)?还是有另一条我没有看到的路径?
我是这样读的:
resyncCh
、stopCh
和 cancelCh
chan
并行等待消息<resyncCh
上收到一条消息,我们会有效地跳转到 r.ShouldResync
的东西,但其他 block 在另一个 chan
还有。stopCh
或 cancelCh
chan
上的任何一点收到消息,返回,有效地“断开”所有 chan
在这里。对吗?
最佳答案
直接回答您的问题:
阻止并行等待消息的 resyncCh、stopCh 和 cancelCh channel 。是的。
如果在 resyncCh 上收到消息,我们实际上会跳转到 r.ShouldResync 内容,但其他 chans 上的其他 block 仍然存在。 不,它们不会保留,您已通过选择
但是,由于此循环,您将再次阻塞。您还可以使用 fallthrough
关键字让它们在通过初始关键字后阻塞。
如果在 stopCh 或 cancelCh chan 上的任何一点收到消息,则返回,有效地与此处的所有 chan“断开连接”。 正确 - 他们会从此函数返回。
此外,请记住您可以使用 default
做什么 --> https://gobyexample.com/non-blocking-channel-operations
for {
select {
case <-resyncCh:
case <-stopCh:
return
case <-cancelCh:
return
default:
fmt.Printf("will keep printing\n")
}
if r.ShouldResync == nil || r.ShouldResync() {
// do stuff
}
resyncCh = r.resyncChan()
}
更新:另一个有用的模式,我现在正在使用,它利用了这一点:
select {
case m := <-c:
handle(m)
case <-time.After(5 * time.Minute):
fmt.Println("timed out")
}
在这里你可以等待,阻塞,在 channel 上,但最终超时,只需要使用 golang time
包。非常简洁易读。将其与具有 timespec
值的 poll()
进行比较。 https://golang.org/pkg/time/#After
关于go - 在以 channel 为特色的 Go case 语句中,阻塞发生在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47211332/
嗯,我的问题是,我想存储某种产品在他们自己的表中,并且从这些产品中我想选择一些到特色/强调产品表,它将存储每个表中所选行的 ID,以在网站的主页上显示这些特色产品。 从表中选择特色产品后,我想自己制定
您好,我正在与 bigcommerce 合作,我希望在每个类别页面上获得以下风格的分割 本质上是制作类别版本; %%Panel.HomeFeaturedProducts%% %%Panel.SideT
在这个网站上http://rwl.rwlwater.com/我有一个小问题...我添加了 overflow: hidden 到特色 slider div,如果启用了 javascript 或用户的互联
我用VB.NET和MySQL作为数据库创建了一个咖啡销售系统。 在该系统中,用户可以更新自己的信息,例如名字、姓氏、BOD 等。 但是,多个用户当前可能会同时编辑同一数据集,这是我需要防止的。 示例:
在这里http://jsfiddle.net/comparebest/yBcKk/6/我有一个 jQuery slider 和两个 Div - DIV ONE 和 DIV TWO。 有没有办法让我自动
我是一名优秀的程序员,十分优秀!