Issue #7097 has been updated by tenderlovemaking (Aaron Patterson).

File thread_variables.patch added

I spoke with ko1-san and Usa-san last night, and we thought that thread_variable_(get|set) would be good (similar to instance_variable_(get|set)).  I've attached an updated patch to make that change.  The documentation includes examples of how the thread local storage and fiber local storage are different.

I added two more methods:

  * Thread#thread_variables # => returns a list of the defined variable keys
  * Thread#thread_variable? # => returns true if a key is set, otherwise false

Thread#local_variable_(get|set) methods respect the same security and frozen behavior as Thread#[] and Thread#[]=.

I think matz said it's OK to add this.  @akr, and @ko1 how is this patch?  If it's OK, I would like to apply to trunk.

Thanks! :-) 
----------------------------------------
Bug #7097: Thread locals don't work inside Enumerator
https://bugs.ruby-lang.org/issues/7097#change-31586

Author: tenderlovemaking (Aaron Patterson)
Status: Assigned
Priority: Normal
Assignee: ko1 (Koichi Sasada)
Category: 
Target version: 
ruby -v: ruby 2.0.0dev (2012-09-25 trunk 37032) [x86_64-darwin12.2.0]


I set a thread local outside an Enumerator.  The Enumerator runs inside the same thread where I set the local.  I would expect the thread local to be available since I am in the same thread, but it is not.

Here is a test that shows the problem:

require 'minitest/autorun'

class ThreadLocalBreaks < MiniTest::Unit::TestCase
  def test_thread_local_in_enumerator
    Thread.current[:foo] = "bar"

    thread, value = Enumerator.new { |y|
      y << [Thread.current, Thread.current[:foo]]
    }.next

    assert_equal Thread.current, thread       # passes
    assert_equal Thread.current[:foo], value  # fails
  end
end



-- 
http://bugs.ruby-lang.org/