Created
September 11, 2009 07:14
-
-
Save takai/185132 to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
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