【问题标题】:main function not waiting for channel read before exiting?主要功能在退出前不等待通道读取?
【发布时间】:2019-09-14 01:40:39
【问题描述】:

考虑以下尝试使用 Go 例程和通道实现 Dining Philosophers

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

问题是为什么会发生这种情况?

我已经知道的:

  1. 如果main go 例程完成,程序将退出(即使其他一些例程仍在运行)。

  2. 如果一个 go 例程尝试从一个通道读取,并且该通道是空的(即之前没有人写过它),它将阻塞。

现在,main 尝试从通道 plates 读取 5 次,因此它不应该终止,直到 philos 例程运行五次。但它似乎在某种程度上仍然设法在这样做之前终止。我错过了什么吗? (似乎plates 只被读取了 4 次。)

编辑:好吧,再想一想,我得出的结论是,philos 例程可能总是运行 5 次,但是,它可以被中断 有时间打印哲学家吃的那个之前。事实上,如果我按如下方式更改顺序,它似乎总是有效:

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
}

不过,如果有人能验证这个解释,那就太好了:)

【问题讨论】:

  • @downvoter:请详细说明这个问题有什么问题?也许我可以尝试改进它。

标签: go goroutine channels


【解决方案1】:

您在标准输出中看到的与正在发生的不一样。有时,mainplates 接收,然后在打印语句发生之前返回。所以:

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 返回之前,有时不会。这并不意味着通道读取没有发生。

【讨论】:

  • 谢谢,再次查看代码后我也得出了这个结论:)
【解决方案2】:

实际上,通道被读取了5次,但是由于main函数只是等待通道被读取5次,所以在philos函数到达这一行之前就退出了:

fmt.Printf("Philosopher # %d finished eating\n", id)`

为了使其正确打印,您需要在写入板通道之前运行此行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2011-03-20
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多