0.0
No commit activity in last 3 years
No release in over 3 years
Makes it easy to create and use thread-local variables.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
 Project Readme

ThreadVariable

Build Status Code Climate

Makes it easy to create and use thread-local variables. This encapsulates two patterns:

  • The Thread.current hash is not namespaced, which can make name collision between different libraries or parts of an app very possible. ThreadVariable gets around this by namespacing variables under the current class/module name.
  • Accessing and setting Thread.current is not so pretty to look at, so ThreadVariable creates a getter and setter for each thread variable.

Installation

Install as your normally would, nothing special here. Only compatible with Ruby 1.9+.

Usage

Simply extend ThreadVariable and call thread_variable in classes you want thread variables in:

class C
  extend ThreadVariable
  thread_variable :foo
end

C.foo = 'bar'

Thread.new do
  C.foo = 'baz'
  C.foo  #=> "baz"
end.join

C.foo  #=> "bar"

If you don’t want to extend ThreadVariable everywhere you can instead do

require 'thread_variable/core_ext'

which will include ThreadVariable in both Class & Module.

For more, just read the source. Seriously, go read it—it’s less than 20 lines and will only take a minute.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Credits & License

Copyright © 2012 J. Andrew Marshall. All rights reserved. License is available in the LICENSE file.