From: Joel VanderWerf Date: 2006-01-17T11:26:13+09:00 Subject: Re: Resetting ruby Luc Heinrich wrote: ... > Another (different) aspect of Tcl which I really like is how event loops > are used to provide ultra-simple interthreads communication with message > queuing and all that jazz: > > % package require Thread > % set worker [thread::create] > % thread::send -async $worker {...some longish calculation...} > % thread::send -async $worker {...some other longish calculation...} > % ...continue on the main thread... > > Rite is supposed to be reentrant, so I guess that it shouldn't be very > hard to build such things upon Ruby 2.0. Oh my, I am *so* looking > forward to it... :) Aside from the use of native threads in this tcl example (IIRC), this is not hard to do in ruby. And with 2.0, we'll have native threads... Anyway here's one way to do worker threads: require 'thread' class WorkerThread < Thread def initialize super do @queue = Queue.new loop do @queue.pop.call end end end def push(&bl) @queue << bl end end def very_long_calculation; (10**10000).to_s.size; end qt = WorkerThread.new results = Queue.new qt.push do results.push(very_long_calculation) end p results.pop -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407