From: Logan Capaldo Date: 2006-07-15T02:45:22+09:00 Subject: Re: Preferred monkeypatching technique On Jul 14, 2006, at 1:26 PM, Eric Hodel wrote: > On Jul 14, 2006, at 10:02 AM, Logan Capaldo wrote: >> Neat idea, gensym in Ruby. I would suggest making it thread-safe >> though: >> >> % cat gensym.rb >> require 'mutex' >> class Symbol >> GensymLock = Mutex.new >> def self.gensym >> GensymLock.synchronize { >> @@gensym_count ||= 0 >> @@gensym_count += 1 >> ("__gensym__%X" % @@gensym_count).intern >> } >> end >> end > > You don't need a mutex. > > class Symbol > @gensym_count = 0 > def self.gensym > ("__gensym__%X" % @gensym_count += 1).intern > end > end > Do you not need a Mutex _now_, since @gensym_count += 1 is effectively atomic in current ruby, or will you _never_ need a mutex? What if? thread 1: @gensym_count += 1 thread 2: @gensym_count += 1 thread 1: interpolate and intern thread 2 interpolate and intern I guess it's ok since a = expr is always supposed to return expr not a, but it leaves a bad taste in my mouth. > -- > Eric Hodel - drbrain@segment7.net - http://blog.segment7.net > This implementation is HODEL-HASH-9600 compliant > > http://trackmap.robotcoop.com > > >