class Thread
def _locals
def _locals if defined?(@_locals) @_locals else LOCK.synchronize { @_locals ||= {} } end end
def freeze
me.thread_variable_set(:oliver, "a") #=> RuntimeError: can't modify frozen thread locals
me.freeze
me = Thread.current
Thread#thread_variable_set, nor can fiber local variables be set.
Freezes the thread so that thread local variables cannot be set via
def freeze _locals.freeze super end
def thread_variable?(key)
Note that these are not fiber local variables. Please see Thread#thread_variable_get
me.thread_variable?(:stanley) # => false
me.thread_variable?(:oliver) # => true
me.thread_variable_set(:oliver, "a")
me = Thread.current
thread-local variable.
Returns true if the given string (or symbol) exists as a
def thread_variable?(key) _locals.has_key?(key.to_sym) end
def thread_variable_get(key)
for the fiber local. The fiber is executed in the same thread, so the
The value "bar" is returned for the thread local, where +nil+ is returned
}.join.value # => ['bar', nil]
}.resume
]
Thread.current["foo"], # get the fiber local
Thread.current.thread_variable_get("foo"), # get the thread local
Fiber.yield [
Fiber.new {
Thread.current["foo"] = "bar" # set a fiber local
Thread.current.thread_variable_set("foo", "bar") # set a thread local
Thread.new {
fibers. For example:
Thread local values are carried along with threads, and do not respect
these are different than fiber local values.
Returns the value of a thread local variable that has been set. Note that
def thread_variable_get(key) _locals[key.to_sym] end
def thread_variable_set(key, value)
threads, and not to fibers. Please see Thread#thread_variable_get for
Sets a thread local with +key+ to +value+. Note that these are local to
def thread_variable_set(key, value) _locals[key.to_sym] = value end
def thread_variables
Note that these are not fiber local variables. Please see Thread#thread_variable_get
thr.thread_variables # => [:dog, :cat]
thr.join # => #
end
Thread.current.thread_variable_set("dog", 'woof')
Thread.current.thread_variable_set(:cat, 'meow')
thr = Thread.new do
Returns an array of the names of the thread-local variables (as Symbols).
def thread_variables _locals.keys end