26 lines
947 B
Text
26 lines
947 B
Text
var [const] forks=(5).pump(List,Atomic.Bool.fp(False)), // True==fork in use
|
|
seats=(5).pump(List,'wrap(n){ List(forks[n],forks[(n+1)%5]) });
|
|
fcn sitAndEat(name,n){ // assigned seating
|
|
while(1){
|
|
fa,fb:=seats[n].shuffle(); // ambidextrous
|
|
if(fa.setIf(True,False)){ // got the first fork
|
|
if(fb.setIf(True,False)){ // got the other fork, nom nom time
|
|
name.println(" is eating");
|
|
Atomic.sleep((1).random(5));
|
|
fa.set(False); fb.set(False); // put forks down
|
|
return(); // leave the table
|
|
}
|
|
else{
|
|
fa.set(False); // put fork down, try again in a bit
|
|
name.println(": Could not get two forks");
|
|
}
|
|
} else name.println(": Could not get first fork");
|
|
Atomic.sleep((1).random(2)); // sits for a bit
|
|
}
|
|
}
|
|
fcn philo([(seat,name)]){ // a thread
|
|
while(1){ // eat and think forever
|
|
name.println(" is thinking."); Atomic.sleep((1).random(5));
|
|
sitAndEat(name,seat);
|
|
}
|
|
}
|