Access the thread-local data from outside : Thread.new « Threads « Ruby






Access the thread-local data from outside


thread = Thread.new do
  t = Thread.current
  t[:var1] = "This is a string"
  t[:var2] = 365
end

x = thread[:var1]               # "This is a string"
y = thread[:var2]               # 365

has_var2 = thread.key?("var2")  # true
has_var3 = thread.key?("var3")  # false

 








Related examples in the same category

1.Create threads in a while loop
2.Pass value into a thread
3.Use upto to create 3 threads
4.manipulate variables from the outer scope
5.True local variables (not accessible from outside)