主要功能在退出前不等待通道读取?

main function not waiting for channel read before exiting?

考虑以下使用 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 例程完成(即使其他一些例程仍在 运行ning),程序将退出。

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

现在,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
}

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

您在标准输出中看到的与实际发生的不同。有时,mainplates 接收,然后在打印语句发生之前从 returns 接收。所以:

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

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

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

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