Skip to content

Instantly share code, notes, and snippets.

@wycats
Created October 7, 2011 03:40
Show Gist options
  • Save wycats/1269388 to your computer and use it in GitHub Desktop.
Save wycats/1269388 to your computer and use it in GitHub Desktop.
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