Skip to content

Commit 72e318a

Browse files
committed
Prepare for Concurrency lecture
1 parent 452c69d commit 72e318a

File tree

5 files changed

+22
-47
lines changed

5 files changed

+22
-47
lines changed

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/s-macke/concepts-of-programming-languages
22

3-
go 1.22.0
4-
5-
toolchain go1.23.0
3+
go 1.23
64

75
require (
86
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210930093333-01de314d7883

src/concurrent/channels/philosophers/build.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
package main
22

3-
import (
4-
"time"
5-
)
6-
73
func main() {
8-
9-
var COUNT = 5
4+
nbrOfSeats := 5
105

116
// start table for 5
12-
table := NewTable(COUNT)
7+
table := NewTable(nbrOfSeats)
138

149
// start philosophers
15-
for i := 0; i < COUNT; i++ {
10+
for i := 0; i < nbrOfSeats; i++ {
1611
philosopher := NewPhilosopher(i, table)
1712
go philosopher.run()
1813
}
19-
go table.run()
20-
21-
// wait 1 millisecond --> check output
22-
time.Sleep(10 * time.Second)
14+
table.run()
2315
}

src/concurrent/channels/philosophers/philosophers.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ func (p *Philosopher) takeForks() {
4949
// try to get forks from table
5050
gotForks := false
5151
for !gotForks {
52-
p.table.getTakeChannel() <- p.id
53-
gotForks = <-p.table.getReservedChannel(p.id)
52+
gotForks = p.table.askForFork(p.id)
5453
}
5554
}
5655

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

6362
// Eating.

src/concurrent/channels/philosophers/table.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,29 @@ type Table struct {
3535

3636
// NewTable constructs a table with n seats.
3737
func NewTable(nbrOfSeats int) *Table {
38-
table := &Table{}
39-
40-
// initialize channels
41-
table.takeCh = make(chan int)
42-
table.putCh = make(chan int)
38+
table := &Table{
39+
nbrOfSeats: nbrOfSeats,
40+
forkInUse: make([]bool, nbrOfSeats),
41+
takeCh: make(chan int),
42+
putCh: make(chan int),
43+
}
4344

4445
table.reservedCh = make([]chan bool, nbrOfSeats)
4546
for i := 0; i < nbrOfSeats; i++ {
4647
table.reservedCh[i] = make(chan bool)
4748
}
48-
table.forkInUse = make([]bool, nbrOfSeats)
49-
table.nbrOfSeats = nbrOfSeats
5049
return table
5150
}
5251

52+
func (t *Table) askForFork(id int) bool {
53+
t.takeCh <- id
54+
return <-t.reservedCh[id]
55+
}
56+
57+
func (t *Table) PutFork(id int) {
58+
t.putCh <- id
59+
}
60+
5361
// Function run() contains the main loop for assigning forks and starting philosophers.
5462
func (t *Table) run() {
5563

@@ -75,15 +83,3 @@ func (t *Table) run() {
7583
}
7684
}
7785
}
78-
79-
func (t *Table) getTakeChannel() chan int {
80-
return t.takeCh
81-
}
82-
83-
func (t *Table) getPutChannel() chan int {
84-
return t.putCh
85-
}
86-
87-
func (t *Table) getReservedChannel(id int) chan bool {
88-
return t.reservedCh[id]
89-
}

0 commit comments

Comments
 (0)