ThreadParent
ThreadParent facilitates spawning threads that maintain a reference to the thread that created them. The primary goal is to allow thread local variable lookup through the ancestor chain.
Installation
Add this line to your application's Gemfile:
gem 'thread-parent'
And then execute:
$ bundle
Or install it yourself as:
$ gem install thread-parent
Usage
Thread is extended to provide direct access to its 'parent', or the thread where the current thread was created. It also provides a way to lookup through its ancestor chain for Thread-local variables.
require 'thread_parent'
Thread.current[:abc] = 'abc'
Thread.new do |thread|
thread.parent == Thread.main #=> true
# Standard local variable lookup behaves as expected.
thread[:abc] #=> nil
# Lookup through the ancestor chain is now supported.
thread.parents[:abc] #=> 'abc'
# Local thread variable assignments works as expected.
thread[:def] = 'def'
thread[:def] #=> 'def'
thread.parents[:def] #=> 'def' <- The calling thread is always checked first.
# Short-hand references to the current thread's parents is also provided.
Thread.parents[:abc] #=> 'abc'
Thread.parent == Thread.main #=> true
Thread.new do
Thread.parents[:def] #= 'def'
# The parent lookup will continue to the parent's parent until a variable is found.
Thread.parents[:abc] #= 'abc'
end
end
Code Status
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request