Skip to content

Instantly share code, notes, and snippets.

@palladin
Created February 20, 2012 14:25
Show Gist options
  • Save palladin/1869460 to your computer and use it in GitHub Desktop.
Save palladin/1869460 to your computer and use it in GitHub Desktop.
Dining philosophers (Joinads)
open System
open FSharp.Extensions.Joinads
// Init
let n = 5
let chopsticks = [| for i = 1 to n do yield new Channel<unit>() |]
let hungry = [| for i = 1 to n do yield new Channel<unit>() |]
let philosophers = [| "Plato"; "Konfuzius"; "Socrates"; "Voltaire"; "Descartes" |]
let randomDelay (r : Random) = System.Threading.Thread.Sleep(r.Next(1, 10) * 1000)
// Fork
for i = 0 to n - 1 do
let left = chopsticks.[i]
let right = chopsticks.[(i+1) % n]
let random = new Random()
join {
match! hungry.[i], left, right with
| _, _, _ ->
printfn "%s is eating" philosophers.[i]
randomDelay random
left.Call(); right.Call()
printfn "%s is thinking" philosophers.[i]
}
// Run
for chopstick in chopsticks do
chopstick.Call()
let random = new Random()
while true do
hungry.[random.Next(0, n)].Call()
randomDelay random
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment