----- Original Message -----
From: "Brian Candler" <B.Candler / pobox.com>
To: "ruby-talk ML" <ruby-talk / ruby-lang.org>
Sent: Monday, March 10, 2003 4:57 PM
Subject: Re: Threads and DRb


> Hmm. DRb uses obj.__send__ to invoke a method. (I don't know what the
> difference is between this and obj.send)
>
> So, in principle you could do:
>
> module ThreadSafe
>   def __tsinit
>     @__tsmutex = Mutex.new
>   end
>   def __send__(*args)
>     @__tsmutex.synchronize { super(*args) }
>   end
> end
>
> When you run this Ruby says:
> threadsafe.rb:5: warning: redefining `__send__' may cause serious problem
>
> but surprisingly it does actually seem to work:
>
>   a = SlowServer.new
>   a.extend ThreadSafe
>   a.__tsinit
>   DRb.start_service('druby://localhost:9000', a)

I'd like Matz's comments on when this redefinition
is/isn't safe.

> I would really like the 'extend ThreadSafe' to perform the initialisation
of
> the instance variable automatically though. Is there a way to do that?

I suppose you could add a line to
__send__:

if not @__tsmutex then @__tsmutex = Mutex.new end

But then that slows down every method call by a
few more microseconds.

Probably a better way, but it doesn't jump out
at me.

Hal