[macruby-changes] [3144] MacRuby/trunk/sample-macruby/Scripts/futures.rb

source_changes at macosforge.org source_changes at macosforge.org
Tue Dec 22 08:39:11 PST 2009


Revision: 3144
          http://trac.macosforge.org/projects/ruby/changeset/3144
Author:   pthomson at 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:
-----------
    MacRuby/trunk/sample-macruby/Scripts/futures.rb

Added: MacRuby/trunk/sample-macruby/Scripts/futures.rb
===================================================================
--- 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20091222/581071fc/attachment.html>


More information about the macruby-changes mailing list