I'm working on a multi-process file processing script. After trying out threads/forking, I found out about IPC (pipe/socket) and last but not least DRb. It seems like the most capable of all the options and relatively user friendly.
I was reading in about thread safety at: https://en.wikibooks.org/wiki/Ruby_Programming/Standard_Library/DRb
But when I tried their examples I don't seem to get a thread-safe result.
Thread safe server:
require 'drb'
require 'thread'
class MyStore
def initialize
@hash = { :counter=>0 }
@mutex = Mutex.new
end
def inc(elem)
@mutex.synchronize do
self[elem] = self[elem].succ
end
end
def [](elem)
@hash[elem]
end
def []=(elem,value)
@hash[elem] = value
end
end
mystore = MyStore.new
DRb.start_service('druby://localhost:9000', mystore)
DRb.thread.join
Client:
require 'drb'
obj = DRbObject.new(nil, 'druby://localhost:9000')
STDOUT.sync = true
100.times do
puts obj[:counter]
obj.inc(:counter)
obj[:lastaccess] = Time.now
end
I'm running the server code first in the background. I later launch the client code twice:
ruby client.rb > 1.txt & ; ruby client.rb > 2.txt
Now I'm expecting to see different numbers in files 1.txt and 2.txt since each client takes control of the counter and doesn't release it until it performed the increment.
What obvious issue am I missing? :)