thread scheduling question

Discussion in 'Ruby' started by Jeffrey Flint, Jan 9, 2008.

  1. This code sends a message around a ring of queues. When each queue
    pops, it prints out its message.

    The code seems to work, but it seems to hang on the io, which I don't
    understand. I would expect the messages to print very quickly. The
    messages only print inside of irb when I depress the carriage return
    repeatedly.

    Thank you!

    Jeff


    =================================
    require 'thread'

    class Ring
    attr_writer :size, :mbox, :agents
    attr_reader :size, :mbox, :agents

    def initialize (size)
    @size = size
    @mbox = []
    @agents = []
    (1..size).each do
    @mbox << Queue.new
    end

    (0..size-1).each do |index|
    @agents << Thread.new(index) do |i|
    loop do
    msg = @mbox.pop
    case msg
    when "start"
    @mbox[(i+1)%size] << 1 if i == 0

    when "halt"
    break

    else
    print("#{i}: #{msg}: #{Thread.current} #{Time.now}\n")
    STDOUT.flush
    (@mbox[(i+1)%size] << msg + 1) if i!=0
    Thread.pass
    end
    end
    end
    end
    end

    def rpc(i, msg)
    @mbox << msg
    end

    def start ()
    rpc(0,"start")
    end

    def halt ()
    @mbox.each { |m| m << "halt" }
    @agents.each {|thr| thr.join }
    end

    end

    R = Ring.new(10)
    R.start

    Attachments:
    http://www.ruby-forum.com/attachment/1266/ring.rb

    --
    Posted via http://www.ruby-forum.com/.
    Jeffrey Flint, Jan 9, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Proctor

    ASP Thread Scheduling Anomalies

    Tony Proctor, Mar 2, 2005, in forum: ASP General
    Replies:
    6
    Views:
    265
    Tony Proctor
    Mar 16, 2005
  2. chris guenther
    Replies:
    0
    Views:
    110
    chris guenther
    Apr 10, 2005
  3. vincent
    Replies:
    0
    Views:
    163
    vincent
    Jun 22, 2005
  4. Replies:
    1
    Views:
    109
    Ara.T.Howard
    Sep 3, 2005
  5. Mark Volkmann

    thread scheduling

    Mark Volkmann, Jan 10, 2006, in forum: Ruby
    Replies:
    5
    Views:
    120
    Lou Vanek
    Jan 11, 2006
Loading...

Share This Page