def thread_local(name, default_value = nil)
      is_a?(Module) or raise TypeError, "receiver has to be a Module"

      name = name.to_s
      my_id = "__thread_local_#{__id__}__"

      ObjectSpace.define_finalizer(self, @@cleanup)

      define_method(name) do
        Thread.current[my_id] ||= {}
        Thread.current[my_id][name]
      end

      define_method("#{name}=") do |value|
        Thread.current[my_id] ||= {}
        Thread.current[my_id][name] = value
      end

      if default_value
        Thread.current[my_id] = {}
        Thread.current[my_id][name] = default_value
      end
      self
    end
def thread_local(name, default_value = nil)
      is_a?(Module) or raise TypeError, "receiver has to be a Module"

      name = name.to_s
      my_id = "__thread_local_#{__id__}__"

      ObjectSpace.define_finalizer(self, @@cleanup)

      define_method(name) do
        Thread.current[my_id] ||= {}
        Thread.current[my_id][name]
      end

      define_method("#{name}=") do |value|
        Thread.current[my_id] ||= {}
        Thread.current[my_id][name] = value
      end

      if default_value
        Thread.current[my_id] = {}
        Thread.current[my_id][name] = default_value
      end
      self
    end
Define a thread local variable named _name_ in this module/class. If the
 value _value_ is given, it is used to initialize the variable.