# Thread scheduling question

**URL:** <https://www.ruby-forum.com/t/thread-scheduling-question/125443>\
**Category:** Ruby\
**Created:** [January 9, 2008, 4:33am UTC](https://www.ruby-forum.com/t/thread-scheduling-question/125443 "2008-01-09T04:33:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jflint](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jflint](https://www.ruby-forum.com/u/jflint)\
**Post date:** [January 9, 2008, 4:33am UTC](https://www.ruby-forum.com/t/thread-scheduling-question/125443/1 "2008-01-09T04:33:27Z")

</div>

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
