Robert Klemme
1/25/2006 12:00:00 PM
Mage wrote:
> Hello,
>
> I was thinking of writing my first threaded app in Ruby. One thread
> should fetch data from the database, another should write it to a
> file.
>
> I would like to use an array, which has hash elements. I append new
> elements to the end, and I write to the file from the beginning
> (FIFO).
>
> My question is that do I really need mutex? Is appending a hash to the
> end of an array atomic, or not? How can I know which method is atomic?
In your case you probably better use a Queue which saves you the
synchronization overhead (the Queue does it internally). Something along
the lines of this (untested):
require 'thread'
QUEUE = Queue.new
threads = [
Thread.new(QUEUE) do |queue|
File.open("foo", "w") do |io|
while x = queue.deq
io.puts x
end
end
end,
Thread.new(QUEUE) do |queue|
while rec = fetch_from_db()
queue.enq rec
end
queue.enc false
end
]
thread.each {|th|th.join}
Note, if you like you can increase the number of DB writer and / or file
reader threads. You can as well leave the db reader in the main loop if
you need only one. Depending on your situation you may need a different
end of processing detection.
Kind regards
robert