Skip to content

Instantly share code, notes, and snippets.

@benstiglitz
Forked from patrickt/futures.rb
Created December 15, 2009 22:15
Show Gist options
  • Save benstiglitz/257358 to your computer and use it in GitHub Desktop.
Save benstiglitz/257358 to your computer and use it in GitHub Desktop.
now with threads
include Dispatch
class Future
def initialize(&block)
@@queue_count ||= 0
Thread.current[:futures_queue] ||= Queue.new("org.macruby.futures-#{Thread.current.object_id}")
@group = Group.new
Thread.current[:futures_queue].async(@group) { @value = block[] }
end
def value
@group.wait
@value
end
end
f = Future.new do
sleep 2.5
'some value'
end
p f.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment