I think that I am facing a fundamental misunderstanding on my part about how threads work in a ruby, and I hope to get some idea.
I would like to have a simple producer and consumer. Firstly, a producer thread that pulls lines from a file and inserts them into SizedQueue; when they end, stick a few tokens on the end to let the consumer (s) know that everything is done.
require 'thread'
numthreads = 2
filename = 'edition-2009-09-11.txt'
bq = SizedQueue.new(4)
producerthread = Thread.new(bq) do |queue|
File.open(filename) do |f|
f.each do |r|
queue << r
end
end
numthreads.times do
queue << :end_of_producer
end
end
Now a few consumers. For simplicity, let them do nothing.
consumerthreads = []
numthreads.times do
consumerthreads << Thread.new(bq) do |queue|
until (line = queue.pop) === :end_of_producer
end
end
end
producerthread.join
consumerthreads.each {|t| t.join}
puts "All done"
, (a) , SizedQueue , , , , () SizedQueue, .
ruby1.9 (ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-darwin9]) . ? , , SizedQueue, .
.