[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [QUIZ] FasterGenerator (#66

Ross Bamford

2/14/2006 3:41:00 PM

On Tue, 2006-02-14 at 01:14 +0900, Jacob Fugal wrote:
> On 2/12/06, Ross Bamford <rossrt@roscopeco.co.uk> wrote:
> > (Incidentally, Jacob, Under 1.8 yours was testing about twice as fast as
> > mine, but it seems to deadlock about half of the time? I have to
> > interrupt after leaving it for up to a minute. I'm on Ruby 1.8.4
> > i686-linux).
>
> Hmm, while I didn't run into any deadlocks with my testing, I'm not
> too surprised. I'm not very threading-savvy and could easily have made
> some critical mistake. :)

It's an easy mistake to make. When you stop threads right away you need
to wait for them in the main thread, otherwise it's possible
(increasingly so the faster a computer you have) that the code following
the Thread.new block will be executed before the new thread gets to the
Thread.stop.

I've been doing some benchmarking and stuff and the following patch
seems to fix it. It passes all the tests and is pretty quick so, nice
work :))

--- jfugal_faster_generator_orig.rb 2006-02-14 15:09:58.000000000 +0000
+++ jfugal_faster_generator.rb 2006-02-14 14:07:17.000000000 +0000
@@ -68,6 +68,7 @@
@block[self]
@mutex.synchronize{ @done = true }
}
+ Thread.pass until @thread.stop?
@thread.run if @thread.status and need_value?
Thread.pass while need_value?
end


--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk