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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?