[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

thread scheduling question

Jeffrey Flint

1/9/2008 3:33:00 AM

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[i].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[i] << 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-...attachment/12...

--
Posted via http://www.ruby-....