Created
October 7, 2011 03:40
-
-
Save wycats/1269388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "thread" | |
$mutex = Mutex.new | |
$total = 0 | |
def incr | |
$mutex.synchronize { $total += 1 } | |
sleep | |
end | |
def total | |
$mutex.synchronize { return $total } | |
end | |
def fib(n) | |
incr if (0..1).include? n | |
fib(n-1) + fib(n-2) if n > 1 | |
end | |
threads = [] | |
1000.times do | |
threads << Thread.new { fib(30) } | |
end | |
sleep 0.5 until total == 1000 | |
system "ps -orss #{Process.pid}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment