douyi9447 2019-04-24 17:21
浏览 85
已采纳

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

Consider the following attempt to implement Dining Philosophers with Go routines and channels.

package main

import "fmt"

func philos(id int, left, right, plate chan bool) {
    fmt.Printf("Philosopher # %d wants to eat
", id) 
    <-left
    <-right
    plate <- true
    left <- true
    right <- true
    fmt.Printf("Philosopher # %d finished eating
", 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
    }   
}

Sometimes this works as expected, i.e. all philosophers eat, e.g.:

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

However, sometimes, one (or more) philosophers are missed (e.g. Philosopher #0, did not eat in the following case):

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

Question is: why does this happen?

What I already know:

  1. The program will exit if the main go routine finished (even if some other routines are still running).

  2. A go routine will block if it tries to read from a channel, and that channel is empty (i.e. no-one wrote to it previously).

Now, main tries to read 5 times from the channel plates, therefore it should not terminate until the philos routine has run five times. But it seems it somehow still manages to terminate before doing that. Am I missing something? (It seems that plates was read only 4 times.)

EDIT: Ok, after thinking about it a bit more, I came to the conclusion, that maybe the philos routine does always run 5 times, however, it can be interrupted before having the time to print that the philosopher ate. Indeed, if I change the order as follows, it seems to be working always:

func philos(id int, left, right, plate chan bool) {
    fmt.Printf("Philosopher # %d wants to eat
", id) 
    <-left
    <-right
    left <- true
    right <- true
    fmt.Printf("Philosopher # %d finished eating
", id) 
    plate <- true
}

Still, it would be great if someone could validate this explanation :)

  • 写回答

2条回答 默认 最新

  • dongwen9051 2019-04-24 17:27
    关注

    What you see in stdout isn't the same as what's happening. Sometimes, main receives from plates and then returns before the print statement happens. So:

    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
    ", id) // this line executes
    

    Because concurrency isn't deterministic, this doesn't happen every time. Sometimes the print happens before main returns, sometimes it doesn't. That doesn't mean the channel read isn't happening.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大