Skip to content

Instantly share code, notes, and snippets.

@edzhelyov
Forked from wycats/threadsplosion.rb
Created October 7, 2011 16:45
Show Gist options
  • Save edzhelyov/1270758 to your computer and use it in GitHub Desktop.
Save edzhelyov/1270758 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