summaryrefslogtreecommitdiff
path: root/sample/philos.rb
diff options
context:
space:
mode:
Diffstat (limited to 'sample/philos.rb')
-rw-r--r--sample/philos.rb9
1 files changed, 4 insertions, 5 deletions
diff --git a/sample/philos.rb b/sample/philos.rb
index 3ccb052532..c38aa4a1cc 100644
--- a/sample/philos.rb
+++ b/sample/philos.rb
@@ -1,14 +1,13 @@
#
# The Dining Philosophers - thread example
#
-require "thread"
srand
#srand
N=9 # number of philosophers
$forks = []
for i in 0..N-1
- $forks[i] = Mutex.new
+ $forks[i] = Thread::Mutex.new
end
$state = "-o"*N
@@ -25,7 +24,7 @@ def eat(n)
end
def philosopher(n)
- while TRUE
+ while true
think n
$forks[n].lock
if not $forks[(n+1)%N].try_lock
@@ -46,8 +45,8 @@ def philosopher(n)
end
end
-for i in 0..N-1
- Thread.start{philosopher(i)}
+for n in 0..N-1
+ Thread.start(n){|i| philosopher(i)}
sleep 0.1
end