Skip to content

Commit

Permalink
Prepare for Concurrency lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
s-macke committed Nov 16, 2024
1 parent 452c69d commit 72e318a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 47 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/s-macke/concepts-of-programming-languages

go 1.22.0

toolchain go1.23.0
go 1.23

require (
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210930093333-01de314d7883
Expand Down
10 changes: 0 additions & 10 deletions src/concurrent/channels/philosophers/build.sh

This file was deleted.

16 changes: 4 additions & 12 deletions src/concurrent/channels/philosophers/main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package main

import (
"time"
)

func main() {

var COUNT = 5
nbrOfSeats := 5

// start table for 5
table := NewTable(COUNT)
table := NewTable(nbrOfSeats)

// start philosophers
for i := 0; i < COUNT; i++ {
for i := 0; i < nbrOfSeats; i++ {
philosopher := NewPhilosopher(i, table)
go philosopher.run()
}
go table.run()

// wait 1 millisecond --> check output
time.Sleep(10 * time.Second)
table.run()
}
5 changes: 2 additions & 3 deletions src/concurrent/channels/philosophers/philosophers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ func (p *Philosopher) takeForks() {
// try to get forks from table
gotForks := false
for !gotForks {
p.table.getTakeChannel() <- p.id
gotForks = <-p.table.getReservedChannel(p.id)
gotForks = p.table.askForFork(p.id)
}
}

// Put forks by channeling our id to the table. The table is responsible for the put logic.
func (p *Philosopher) putForks() {
fmt.Printf("Philosopher #%d puts down forks\n", p.id)
p.table.getPutChannel() <- p.id
p.table.PutFork(p.id)
}

// Eating.
Expand Down
34 changes: 15 additions & 19 deletions src/concurrent/channels/philosophers/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,29 @@ type Table struct {

// NewTable constructs a table with n seats.
func NewTable(nbrOfSeats int) *Table {
table := &Table{}

// initialize channels
table.takeCh = make(chan int)
table.putCh = make(chan int)
table := &Table{
nbrOfSeats: nbrOfSeats,
forkInUse: make([]bool, nbrOfSeats),
takeCh: make(chan int),
putCh: make(chan int),
}

table.reservedCh = make([]chan bool, nbrOfSeats)
for i := 0; i < nbrOfSeats; i++ {
table.reservedCh[i] = make(chan bool)
}
table.forkInUse = make([]bool, nbrOfSeats)
table.nbrOfSeats = nbrOfSeats
return table
}

func (t *Table) askForFork(id int) bool {
t.takeCh <- id
return <-t.reservedCh[id]
}

func (t *Table) PutFork(id int) {
t.putCh <- id
}

// Function run() contains the main loop for assigning forks and starting philosophers.
func (t *Table) run() {

Expand All @@ -75,15 +83,3 @@ func (t *Table) run() {
}
}
}

func (t *Table) getTakeChannel() chan int {
return t.takeCh
}

func (t *Table) getPutChannel() chan int {
return t.putCh
}

func (t *Table) getReservedChannel(id int) chan bool {
return t.reservedCh[id]
}

0 comments on commit 72e318a

Please sign in to comment.