From: Robert Dober Date: 2007-06-15T17:21:59+09:00 Subject: Re: Reasonable practice? On 6/15/07, Trans wrote: > It the following reasonable? How thread safe is it? > > class X > attr :type > > def initialize(ary) > @ary = ary > end > > def go > @ary.each { |x| > @type = x -------------------------> Problem, Thread 1 gets interrupted by Thread2 -------------------------> Thread2 changes @type > run > } > end > > def run > puts type > end > > end > > X.new(['a', 'b', 'c']).go > Tom I do not know how big the chance is that the scenario above will happen but it is *not* thread safe. I just recently was awoken by Robert Klemme about instance variable issues, so I am not an expert. Could you come up with passing local variables around as params. The method calls might become ugly :( or your methods longer :((. In the simplfied code it will work out nicely but how will it scale to your real application. I am working on closure properties right now -- they will not solve your problem yet, but I was thinking a lot about lockable properties, this will look like this require 'labrador/properties' class X extend Properties property :type, :lockable def go ... lock_property :type, :lock_type => :write_lock self.type = x run ensure unlock_property :type but my mileage on this will be bad (1) as I am not a Thread guru and I do not want to reinvent the wheel, but I am thinking about the BusyFlag class from Java Threads. BTW if you look for performance just forget it, I expect a performance penalty of factor 5 :( for closure based properties. Cheers Robert (1) and non lockable properties are not ready yet :( -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw