September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
26
Task/Dining-philosophers/Zkl/dining-philosophers-1.zkl
Normal file
26
Task/Dining-philosophers/Zkl/dining-philosophers-1.zkl
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
4
Task/Dining-philosophers/Zkl/dining-philosophers-2.zkl
Normal file
4
Task/Dining-philosophers/Zkl/dining-philosophers-2.zkl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
T("Aristotle", "Kant", "Spinoza", "Marx", "Russell").enumerate()
|
||||
.apply(philo.launch);
|
||||
|
||||
Atomic.sleep(100000); // hang out in the REPL, aka thread keep alive
|
||||
Loading…
Add table
Add a link
Reference in a new issue