Dining Philosophers

From Rholang Wiki
Jump to navigation Jump to search

The dining philosophers is a classic problem in concurrent computation with a scarce resource. Elegant solutions to the problem exist, but are unweildy to express in most programming languages. Rholang's join operator shines here.

Other resources[edit]

Joshy Orndorff has written about this problem in detail. A Korean translation is also available.

Mike's tutorial (Hard to find this link. Used to live at developer.rchain.coop/tutorial

Solution in rholang[edit]

This solution is written in simplified syntax. All external resources show a solution in coop rholang.


 new stdout(`rho:io:stdout`), north, south, knife, spoon in {
   // Set the table
   north!(knife) |
   south!(spoon) |
 
   // Philosopher 1's plan
   for (knf <- north) {
     for (spn <- south) {
       stdout!("Philosopher 1 is full.") |
       north!(knf) |
       south!(spn)
     }
   } |
 
   // Likewise for philosopher 2
   for (spn <- south) {
     for (knf <- north) {
       stdout!("Philosopher 2 is full.") |
       north!(knf) |
       south!(spn)
     }
   }
 }