Revision
3144
Author
pthomson@apple.com
Date
2009-12-22 08:39:08 -0800 (Tue, 22 Dec 2009)

Log Message

Added an implementation of futures (also called promises or delayed computations) on top of GCD.

Added Paths

Diff

Added: MacRuby/trunk/sample-macruby/Scripts/futures.rb (0 => 3144)


--- MacRuby/trunk/sample-macruby/Scripts/futures.rb	                        (rev 0)
+++ MacRuby/trunk/sample-macruby/Scripts/futures.rb	2009-12-22 16:39:08 UTC (rev 3144)
@@ -0,0 +1,22 @@
+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
\ No newline at end of file