From: Earle Clubb Date: 2008-07-22T04:12:46+09:00 Subject: Thread-local global variables? Is it possible to define a variable whose value is global within a particular each thread (including the main thread), yet distinct between threads? For example, I've tried this: $abc = 0 threads = [] 2.times do |i| threads << Thread.new(i+1) do |i| $abc = i 10.times do print "thread #{i}: abc = #{$abc}\n" sleep 0.5 end end end 10.times do print "thread 0: abc = #{$abc}\n" sleep 0.5 end threads.each {|t| t.join} Of course it prints: thread 1: abc = 1 thread 2: abc = 2 thread 0: abc = 2 thread 1: abc = 2 thread 0: abc = 2 thread 2: abc = 2 thread 0: abc = 2 thread 2: abc = 2 thread 1: abc = 2 ... Is there any way to do this so that it prints like this? thread 1: abc = 1 thread 2: abc = 2 thread 0: abc = 0 thread 1: abc = 1 thread 0: abc = 0 thread 2: abc = 2 thread 0: abc = 0 thread 2: abc = 2 thread 1: abc = 1 ... Thanks, Earle -- Posted via http://www.ruby-forum.com/.