Programming generators with threads 
Author Message
 Programming generators with threads

Hi, in Rubytalk 55895 [
http://www.*-*-*.com/ ] Matz
wrote that generators can be written with Threads as well as with
Callcc.  He then gave an example with Calcc.  Works fine.  Here is my
example with Threads.  But I'm not sure whether it is good programming.
Is it free of race conditions, for example?  Thanks.

module Math
   def Math.divisible(number)
      square_root = Math.sqrt(number).floor
      for divisor in 2..square_root
         return true if number % divisor==0
      end
      return false
   end
end

class PrimeGenerator

   def initialize

         loop do # this thread is an infinite loop..

            Thread.stop # going to sleep, goodnight everybody

            # Uh oh ... Brrr.  Where am I?
            # I have been woken up and scheduled to run.
            # See (*)

            Thread.critical = true # take exclusive right to run
            # this right is necessary, because I must be kept awake
            # by scheduler until next prime is computed
            complex_computation # do something interesting..
            Thread.critical = false # release exclusive right
         end
      }

   end

   def next


   end

   def complex_computation


   end

end

p = PrimeGenerator.new
puts p.next #> 2
puts p.next #> 3
puts p.next #> 5
puts p.next #> 7



Fri, 18 Nov 2005 23:49:51 GMT  
 Programming generators with threads

Quote:

> Hi, in Rubytalk 55895 [
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/55895 ] Matz
> wrote that generators can be written with Threads as well as with
> Callcc.  He then gave an example with Calcc.  Works fine.  Here is my
> example with Threads.  But I'm not sure whether it is good programming.
> Is it free of race conditions, for example?  Thanks.

I tend to prefer using callcc for generators, because there are fewer
messy issues with garbage collection (ideally the thread should be
killed if the generator gets garbage collected).

Quote:
> class PrimeGenerator

>    def initialize


Probably a good idea to use Thread.current.abort_on_exception=true here,
in case an exception escapes from this thread.

Ideally, if an exception does escape, it should be passed from this
thread to the thread that called next().  That's exactly what I did in:
  http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/rubycollections/rubyco...

Quote:
>          loop do # this thread is an infinite loop..

>             Thread.stop # going to sleep, goodnight everybody

>             # Uh oh ... Brrr.  Where am I?
>             # I have been woken up and scheduled to run.
>             # See (*)

>             Thread.critical = true # take exclusive right to run

It's a good idea to use Thread.exclusive instead of Thread.critical
here, in case complex_computation raises an exception:

  Thread.exclusive do
    complex_computation
  end

The disadvantage of using Thread.critical/exclusive is that no other
threads can run.  If complex_computation takes a long time to run, then
this is probably not good.

Quote:
>             # this right is necessary, because I must be kept awake
>             # by scheduler until next prime is computed
>             complex_computation # do something interesting..
>             Thread.critical = false # release exclusive right
>          end
>       }

>    end

>    def next




before complex_computation finishes.  You probably want to use

to let us know the next number is ready to be returned.

Quote:
>    end

>    def complex_computation


>    end

> end

Paul


Sat, 19 Nov 2005 02:55:02 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Continuations and threads (was Re: Iterators & generators)

2. Are generator functions thread-safe?

3. multi-threaded *nix program needs to spawn an external program/script

4. Help: Fortran program as a thread of a C program

5. Threads creating threads creating threads...

6. thread, threading, mutex modules and non-threading interpreters

7. Wanted : Recommendations on Clipper Program Generators

8. Softcode / Program generators

9. LFSR generator program

10. LFSR generator program

11. automatic VHDL generator program

12. ANNOUNCE: `tg', a test driver generator for Ada programs

 

 
Powered by phpBB® Forum Software