Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Created October 2, 2014 13:58
Show Gist options
  • Save yorickpeterse/c882dc0293ae55ebd346 to your computer and use it in GitHub Desktop.
Save yorickpeterse/c882dc0293ae55ebd346 to your computer and use it in GitHub Desktop.
require 'thread'
input = [10, 12, 14, 18]
output = []
threads = []
lock = Mutex.new
def fib(n)
return n < 2 ? n : fib(n-1) + fib(n-2)
end
10.times do |index|
threads << Thread.new do
input.each do |some_value|
lock.synchronize { output << fib(some_value) }
end
end
end
threads.each(&:join)
p output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment