Thread-local accessors for your classes
Switch branches/tags
Nothing to show
Pull request Compare This branch is 1 commit ahead of scottpersinger:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Thread-local accessors for your classes

Yet another tiny library to tackle this problem.

Install with: gem install schoefmax-tlattr_accessors --source=


require 'rubygems'
require 'tlattr_accessors'

class ThreadExample
  extend ThreadLocalAccessors
  tlattr_accessor :foo

  def test = "bla" {
      puts foo                  # prints "nil" = "blubb"
      puts foo                  # prints "blubb"
    puts foo                    # prints "bla"

If you want to enable them globally, add this somewhere (e.g. an initializer in Rails)

Object.send :extend, ThreadLocalAccessors

Default values

Adding true as last parameter will cause the first value set on the attribute to act as default value for all other threads:

tlattr_accessor :yeah, :baby, true

def test_default
  self.yeah = "bla" {
    puts yeah                  # prints "bla"
    puts baby                  # prints "nil" = "blubb"
    self.yeah = "blabla"
  puts yeah                    # prints "bla"
  puts baby                    # prints "blubb"

Getters and Setters

This gem doesn't support tlattr or tlattr_reader|writer for the simple reason that they don't make any sense here (you don't have an “instance variable”, so you need both methods). If you want to hide one of them from your API, you can always make them private:

tlattr_accessor :foo
private :foo= # hide the setter


The Thread.current-Hash is a global namespace. Using it to store thread-local variables safely requires carefully crafted keys, which tend to be rather expensive to compute. This hurts performance if the attribute is accessed frequently. Therefore, this library uses a different approach, which is a lot faster: The values are stored in a separate hash which is keyed by the object_id of the thread. Finalizers make sure no memory is leaked when threads finish (see the spec).

Running specs

If you haven't already, install the rspec gem, then run:

spec spec

© 2009, Max Schoefmann <max (a) pragmatic-it de> Released under the MIT license