From: Robert Klemme Date: 2004-06-11T02:58:37+09:00 Subject: Re: a __real__ drb question *** forget the other posting, I accidentally hit 'send' *** "Ara.T.Howard" schrieb im Newsbeitrag news:Pine.LNX.4.60.0406100854460.10410@harp.ngdc.noaa.gov... > > now that i've already stuck my foot in my mouth, i'd like to ask a real drb > question. > > say you've got a thread accessing an obj which is being served by drb: > > require 'drb/drb' > > mode = ARGV.shift || 'server' > > class Q > def initialize; @q=[]; end > def push obj; @q << obj; end > def pop; @q.shift; end > end > > case mode > when 'server' > q = Q.new > # > # is this safe? > # No. > Thread.new{loop{ sleep 2; q.push Time.now }} > > DRb.start_service nil, q > uri = DRb.uri > puts "ruby #{ $0 } client #{ DRb.uri }" > DRb.thread.join > > when 'client' > uri = ARGV.shift > DRb.start_service nil, nil > q = DRbObject.new nil, uri > loop do > obj = q.pop > p obj > q.push 42 > sleep 0.5 > end > end > > > my understanding is that access to the object via DRb is provided by multiple > synchronized threads. eg. only one DRb thread would be accessing your object > at once so you generally don't need to consider thread coordination. That was not my understanding and tests back this notion: DRB started at druby://localhost:8787 #: #: ++++ [0] #: #: ++++ [0] #: #: ++++ [0] #: #: ++++ [0] #: #: ++++ [0] #: #: ---- [0] #: #: ++++ [1] #: #: ---- [0] #: #: ---- [0] #: #: ++++ [1] #: #: ++++ [1] #: #: ---- [0] #: #: ++++ [1] #: #: ---- [1] #: #: ++++ [2] #: #: ---- [1] .... (server output, see code attached) > in this > case, however, i have a thread outside of DRb accessing the object - what sort > of coordination would this require? perhaps it would be better to have the > thread obtain a DRbObject itself and allow the DRb library to handle all the > coordination? A general note about concurrency that applies here as well: Typically "automated" synchronization is not done because no automated mechanism can know the granularity of concurrency needed. You might even access an instance concurrently in a proper way without synchronization (e.g. if the instance does not contain any state itself). Another case is a set of operations that has to be done exclusively; a typical example is check for presence of something and then act depending on the presence like in: mutex.synchronize do if foo.has_data? puts foo.get_data else foo.set_data "something" end end That's why typically no synchronization is done automatically. Kind regards robert