Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,41 @@
import threadpool, locks, math, os
randomize()
type Philosopher = ref object
name: string
forkLeft, forkRight: int
const
n = 5
names = ["Aristotle", "Kant", "Spinoza", "Marx", "Russell"]
var
forks: array[n, TLock]
phils: array[n, Philosopher]
threads: array[n, TThread[Philosopher]]
proc run(p: Philosopher) {.thread.} =
sleep random(1 .. 10) * 500
echo p.name, " is hungry."
acquire forks[min(p.forkLeft, p.forkRight)]
sleep random(1 .. 5) * 500
acquire forks[max(p.forkLeft, p.forkRight)]
echo p.name, " starts eating."
sleep random(1 .. 10) * 500
echo p.name, " finishes eating and leaves to think."
release forks[p.forkLeft]
release forks[p.forkRight]
for i in 0 .. <n:
initLock forks[i]
phils[i] = Philosopher(
name: names[i],
forkLeft: i,
forkRight: (i+1) mod n)
threads[i].createThread run, phils[i]
threads.joinThreads