2013-02-04 Austin Ruby

Tonight I went to Austin Ruby. At the end there was a pairing exercise.

The kata can be found on the Austin Ruby github page. It involved threads. I thought the point of using a high-level language like Ruby was to never deal with threads.

require 'thread'

p_count = ARGV[0]
if p_count.nil?
  p_count = "2"
end
p_count = p_count.to_i

queue = Queue.new

puts "starting pusher"
pusher = Thread.new do
  i = 0
  while(true) do
    queue << i
    sleep 0.5
    # puts "Adding #{i} to queue"
    i = i + 1
    true
  end
end

sleep 20

puts "starting pulls"
pullers = []
y = 0
p_count.times do

  y = y + 1
  t = Thread.new {
    while true do
      puts "#{Thread.current.object_id}: #{queue.pop}"
      sleep(rand(0..2))
    end
  }
  pullers << t
  sleep 0.7
  t.run
  puts "New thread"
end

while true do
  # It is supposed to print forever  
end

Image from World Digital Library, assumed allowed under Fair Use. Image from the Ashburnham Pentateuch, or Tours Pentateuch, a Latin manuscript of the first five books of the Old Testament from the 6th century or 7th century. Its place of origin is unknown.

 

There are 78 words in this article.