Озадаченный этими проблемами с параллелизмом - PullRequest
0 голосов
/ 22 ноября 2018

Я только что закончил курс "Параллелизм в движении" на Coursera (https://www.coursera.org/learn/golang-concurrency/), и я действительно боролся с последними заданиями. Это мое представление:

// 1.There should be 5 philosophers sharing chopsticks, with one chopstick between each adjacent pair of philosophers.
// 2.Each philosopher should eat only 3 times (not in an infinite loop as we did in lecture)
// 3.The philosophers pick up the chopsticks in any order, not lowest-numbered first (which we did in lecture).
// 4.In order to eat, a philosopher must get permission from a host which executes in its own goroutine.
// 5.The host allows no more than 2 philosophers to eat concurrently.
// 6.Each philosopher is numbered, 1 through 5.
// 7.When a philosopher starts eating (after it has obtained necessary locks) it prints “starting to eat <number>” on a line by itself, where <number> is the number of the philosopher.
// 8.When a philosopher finishes eating (before it has released its locks) it prints “finishing eating <number>” on a line by itself, where <number> is the number of the philosopher.
package main

import (
    "fmt"
    "sync"
)

var eating = make(chan int, 2)

var mu sync.RWMutex
var everyoneAte int
var timesEaten = make(map[int]int, 5)

type chopstick struct {
    sync.Mutex
}

type philosopher struct {
    leftCs  *chopstick
    rightCs *chopstick
}

func alreadyAte(index int) bool {
    mu.Lock()
    defer mu.Unlock()
    if timesEaten[index] == 3 {
        return true
    }
    return false
}
func (p philosopher) eat(index int) {

    eating <- 1

    p.leftCs.Lock()
    p.rightCs.Lock()

    fmt.Printf("Starting to eat %v\n", index)
    fmt.Printf("Finishing eating %v\n", index)
    mu.Lock()
    timesEaten[index]++
    if timesEaten[index] == 3 {
        everyoneAte++
    }
    mu.Unlock()
    p.rightCs.Unlock()
    p.leftCs.Unlock()
    <-eating
}

func main() {
    count := 5
    chopsticks := make([]*chopstick, count)
    for i := 0; i < count; i++ {
        chopsticks[i] = &chopstick{}
    }

    philosophers := make([]*philosopher, count)
    for i := 0; i < count; i++ {
        philosophers[i] = &philosopher{
            leftCs:  chopsticks[i],
            rightCs: chopsticks[(i+1)%count],
        }
    }
    for {
        mu.RLock()
        if everyoneAte == count {
            return
        }
        for i := 0; i < count; i++ {
            if timesEaten[i] == 3 {
                continue
            }
            go philosophers[i].eat(i + 1)
        }
        mu.RUnlock()
    }

}

Я не понял, какреализовать # 4, поэтому я просто использовал вместо этого буферизованный канал ?‍♂️

Я не понимаю, почему после возвращения некоторые философы ели более 3 раз ?

Если у всех есть ответы наэти вопросы, я был бы признателен. Я уже представил задание.

1 Ответ

0 голосов
/ 23 ноября 2018

Вот последовательность, в которой философы могут есть более трех раз

Предположим, что философ_ ел 2 раза

  1. main: приобретает RLock
  2. main:читает timesEaten[1] == 2
  3. main: делает philosopher_1 есть на отдельной карте_1
  4. main: выпуски RLock
  5. main: приобретает RLock
  6. main: читает timesEaten[1] == 2
  7. main: заставляет philosopher_1 снова есть на отдельной карте_2
  8. main: релизы RLock
  9. goroutine_1: получает Lock
  10. goroutine_1: наборы timesEaten[1] = 3
  11. goroutine_1: релизы Lock
  12. goroutine_2: приобретение Lock
  13. goroutine_2: наборы timesEaten[1] = 4 <-<code>philosopher_1 съели более 3 раз
  14. goroutine_2: релизы Lock
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...