From: Mike Bethany Date: 2011-06-18T08:19:37+09:00 Subject: Re: Threadsafing a mixin module without using self.new or initialize. --90e6ba5bc0879434dd04a5f09a5d Content-Type: text/plain; charset=ISO-8859-1 On Fri, Jun 17, 2011 at 5:45 PM, Robert Klemme wrote: > Since this is a mixin module I can't use initialize to create the mutex >> instance variable >> > > Why not? > > module Anything > def initialize(*a,&b) > super > @lock = Mutex.new > @data = [] > end > end > > class Other > include Anything > > def initialize > super # important! > @another_var = 15 > end > end > > [smacks forehead] Doh! I'm still learning Ruby, didn't even think about that. I like the initialize method MUCH better. If they forget to include the super it'll raise an exception when they go to use it so I just need to make sure I document and emphasize that fact really well. > That approach does not scale well if you have lots of instances created > concurrently. If anything fails you can add a method which creates the > mutex and which is called before the instance is used by multiple threads - > as simple as that. But I'd prefer the approach with #initialize. > Yeah, I thought about that and it was why I didn't really like my solution. Limiting creation at the class level could be a major bottleneck. Plus it just felt hackey; but not in a good way. > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.**com/ Thanks for the help! --90e6ba5bc0879434dd04a5f09a5d--