主函数提前退出不等待通道读取?
时间:2024-02-28
浏览:315
作者:佚名
知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《主函数提前退出不等待通道读取?》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!
考虑以下使用 go 例程和通道来实现哲学家就餐的尝试。
package main
import "fmt"
func philos(id int, left, right, plate chan bool) {
fmt.printf("philosopher # %d wants to eat\n", id)
<-left
<-right
plate <- true
left <- true
right <- true
fmt.printf("philosopher # %d finished eating\n", id)
}
func main() {
const numphilos = 5
var forks [numphilos]chan bool
for i := 0; i < numphilos; i++ {
forks[i] = make(chan bool, 1)
forks[i] <- true
}
plates := make(chan bool)
for i := 0; i < numphilos; i++ {
go philos(i, forks[(i-1+numphilos)%numphilos], forks[(i+numphilos)%numphilos], plates)
}
for i := 0; i < numphilos; i++ {
<-plates
}
}
有时这会按预期工作,即所有哲学家都吃饭,例如:
philosopher # 4 wants to eat philosopher # 3 wants to eat philosopher # 2 wants to eat philosopher # 1 wants to eat philosopher # 4 finished eating philosopher # 3 finished eating philosopher # 2 finished eating philosopher # 1 finished eating philosopher # 0 wants to eat philosopher # 0 finished eating
然而,有时,一个(或多个)哲学家会被遗漏(例如,哲学家#0,在以下情况下没有吃饭):
philosopher # 4 wants to eat philosopher # 1 wants to eat philosopher # 3 wants to eat philosopher # 2 wants to eat philosopher # 4 finished eating philosopher # 0 wants to eat philosopher # 2 finished eating philosopher # 1 finished eating philosopher # 3 finished eating
问题是:为什么会发生这种情况?
我已经知道的:
如果
maingo 例程完成,程序将退出(即使其他例程仍在运行)。如果 go 例程尝试从某个通道读取数据,并且该通道为空(即之前没有人写入),则它会阻塞。
现在,main 尝试从通道 plates 读取 5 次,因此在 philos 例程运行五次之前它不会终止。但似乎它仍然设法在这样做之前终止。我错过了什么吗? (看来plates只被读取了4次。)
编辑:好的,经过更多思考,我得出结论,也许 philos 例程总是运行 5 次,但是,它可以之前< /strong> 有时间打印哲学家吃的东西。事实上,如果我按如下方式更改顺序,它似乎总是有效:
func philos(id int, left, right, plate chan bool) {
fmt.Printf("Philosopher # %d wants to eat\n", id)
<-left
<-right
left <- true
right <- true
fmt.Printf("Philosopher # %d finished eating\n", id)
plate <- true
}
不过,如果有人可以验证这个解释那就太好了:)
解决方案
您在标准输出中看到的内容与正在发生的情况不同。有时,main从plates接收,然后在打印语句发生之前返回。所以:
plate <- true
left <- true // on this line or on
right <- true // this line, main receives from plate and then returns before
fmt.printf("philosopher # %d finished eating\n", id) // this line executes
因为并发不是确定性的,所以这种情况不会每次都会发生。有时打印发生在 main 返回之前,有时则不会。这并不意味着通道读取没有发生。
实际上,通道被读取了 5 次,但由于 main 函数只等待第 5 次读取通道,因此它退出在 philos 函数到达此行之前:
fmt.Printf("Philosopher # %d finished eating\n", id)`
为了使其正确打印,您需要在写入板通道之前运行此行。
今天关于《主函数提前退出不等待通道读取?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
