Skip to content

Instantly share code, notes, and snippets.

@takai
Created September 11, 2009 07:14
Show Gist options
  • Save takai/185132 to your computer and use it in GitHub Desktop.
Save takai/185132 to your computer and use it in GitHub Desktop.
require 'thread'
class Future
def initialize &block
@cv = ConditionVariable.new
@mutex = Mutex.new
Thread.start do
@result = block.call
@cv.signal
end
end
def get
@mutex.synchronize do
unless @result
@cv.wait(@mutex)
end
end
@result
end
def done?
@result ? true : false
end
end
if __FILE__ == $0
def fib(n)
n < 2 ? 1 : fib(n-1) + fib(n-2)
end
task = Future.new do
fib 30
end
puts "task.done? #=> %s" % task.done?
puts "task.get #=> %d" % task.get
puts "task.done? #=> %s" % task.done?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment