S.Z. wrote:
> I am playing with a Synchronized class that automates resource locking
> (code snippet can be found here:
> http://www.rubygarden.org/ruby?MultiThreading).
> One issue arised is how to overload all (or particular) Object's (or
> some ancestor's) methods by the automated way?
> Syncronized extraction looks like this:
>
> class Syncronized
>         def initialize( klass, *args )
>                 @obj= klass::new( *args );
>         end
>
>         def to_s() @obj.to_s(); end
>         # What about clone(), taint(), and other?
> end
>
> It is hard work to type all the Objecs's methods by hand in spite of
> their bodies are the same.
> What is the better choice?

You can iterate all instance methods, alias each one and create a new one
calling the original one synchronized.  Delegate might be helpful, too.

Note though that automatically synchronizaton of each method is not
necessarily going to make your program thread safe.  Every transaction
that involves more than one method invocation won't be thread safe with
this approach - in this case you need explicit external synchronization:

# typical example
unless hash.contains_key? "foo"
  # lengthy calculation
  hash["foo"] = result_of_lenthy_calculation
end

Other typical examples involve scenarios where several instances have to
be changed consistently.  All these are reasons why Sun stepped away from
full synchronization (Vector, StringBuffer) and added classes that do not
employ method level synchronization (ArrayList, StringBuilder), which is
more performant.

Kind regards

    robert